Search code examples
linuxescapingwhitespaceksh

Whitespace as an argument of a ksh script


Suppose you have the following script:

#!/usr/bin/ksh

for element in $@
    do
            echo $element
done

And you execute the script using:

./myscript.ksh "my element"

The expected output should be:

my element

but the whitespace is treated as a separator for each argument, obtaining:

my
element

How should I escape the whitespace?

Thanks


Solution

  • You must either quote the $@, or use nothing:

    for element in "$@"
    

    or

    for element
    

    will both work.