Not all ksh scripts will run in Bash. Bash has "borrowed" a few features that originated from ksh, but it doesn't support everything ksh(93) has.
The most obvious difference is in the handling of local variables: ksh's function foo
functions support static scoping, while Bash supports dynamic scoping. Even though Bash supports the same style of function declarations, and ksh's typeset
command, they're not compatible.
The following script runs in both, but behaves differently:
% cat local.sh
function g {
echo "in g: '$var'"
}
function f {
typeset var="value from f"
echo "in f: '$var'"
g
}
typeset var="value from main"
f
echo "in main: '$var'"
in ksh, function g
sees the value set in the main level, while in Bash, it sees the value set in function f
:
% ksh local.sh
in f: 'value from f'
in g: 'value from main'
in main: 'value from main'
% bash local.sh
in f: 'value from f'
in g: 'value from f'
in main: 'value from main'
With foo()
functions, ksh's typeset
would just modify the global value. Zsh and many other shells behave just like Bash, here. (Though they might only support local
and not typeset
. Ksh doesn't have local
.)
Another thing is that ksh supports nested arrays (and IIRC, some other data structures too). Bash doesn't, and will just croak for e.g. this script:
% cat nested.sh
a=((a b c) (d e f))
typeset -p a
echo "a[1][*] is '${a[1][*]}'"
echo "a[1][2] is '${a[1][2]}'"
% ksh nested.sh
typeset -a a=((a b c) (d e f) )
a[1][*] is 'd e f'
a[1][2] is 'f'
There may be other, minor differences, like read -A
vs. read -a
for reading in an array, and ksh doesn't have Bash's mapfile
/readarray
(at least not with the same name). Of course Bash also has a few Bash-specific features, like all the BASH*
variables.
(The above scripts were run with the ksh on my Mac, where ksh --version
says sh (AT&T Research) 93u+ 2012-08-01
)
zsh
? - Unix & Linux Stack Exchange