I'm trying to get a (bourne shell) variable to expand like "$@" so it produces multiple words with some having preserved spaces. I've tried defining the variable in many different ways but still can't get it to work:


n=\"one\ two\"\ three
for i in "$n"; do
echo $i

I want to define the variable so the script outputs one two first and then three next iteration, which is what you'd get if you replaced the quoted variable with "$@" and passed 'one two' three as the arguments.

Is "$@" just magic?


So bourne shell (IIRC) doesn't support arrays. You can still use "$@"

set -- "one two" three
for i in "${@}" ; do
    echo "$i"


one two

Tested on AIX 7.1 bsh.

  • thanks, this does the trick. I guess this means "$@" is magic. – user126144 Aug 3 '15 at 23:09
  • 2
    @user126144 Yes, it is. "$@" is the only thing that expands to several words despite being in double quotes. (Shells that support arrays have the same thing with "${array_variable[@]}") – Gilles 'SO- stop being evil' Aug 3 '15 at 23:17
  • This answer  discusses arrays in bash, ksh, zsh, and yash, – Scott Jun 20 '17 at 1:35
  • Alternatively, printf '%s\n' "$@" – Kusalananda Jan 13 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.