Question

I have this KornShell (ksh) script. It is printing only a and b. I am not sure why it is printing only these two letter?

script:

#!/bin/ksh
for i in [global]
do
        echo $i
done
exit 0

output:

./a.sh
a
b

I need to understand how shell interpreting [global] word.

Was it helpful?

Solution

Because the string [global] is treated by the shell as a glob pattern and attempts to match any files in the current directory that have a single-character name from the set of characters {a, b, g, l, o}. You have two files in that directory named a and b respectively. If you want [global] to be treated as a string, then add some quotes:

for i in "[global]"
do
  echo "$i"
done

Quoting prevents globbing.

OTHER TIPS

This ksh script works fine in Cygwin with ksh version: MIRBSD KSH R48 2013/08/16.

Ksh Script

#!/bin/ksh
for i in [global]
do
    echo ${i}
done
exit 0

Ksh Script Output:

userr@foo:/tmp $ ksh so7.ksh
ksh so7.ksh
[global]
userr@foo:/tmp $ echo $KSH_VERSION
echo $KSH_VERSION
@(#)MIRBSD KSH R48 2013/08/16
userr@foo:/tmp $ sh so7.ksh
sh so7.ksh
[global]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top