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?
"$@"
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 03 '15 at 23:17printf '%s\n' "$@"
– Kusalananda Jan 13 '19 at 14:10