Skip to main content
Tweeted twitter.com/#!/StackUnix/status/628489369683427328
edited tags
Link
Gilles 'SO- stop being evil'
  • 865.4k
  • 205
  • 1.8k
  • 2.3k
Source Link

"$@" expansion for user defined variables

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:

#!/bin/sh

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

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?