Running this bash script running gives two different results. Can anyone explain why? Is there a better "bash one liner" to duplicate "$@" into a new variable that then behaves to same way when iterated over in a for loop using the same loop syntax? (An array requires different syntax to loop over.)
#!/bin/bash
# run as: bash thisScript.bash "arg one" "arg two"
args="$@"
echo 'Testing @:'
for i in "$@" ; do
echo "$i"
done
echo 'Testing args:'
for i in "$args" ; do
echo "$i"
done
I did find the following quote, but it doesn't fully explain what's happening, and it isn't helpful in duplicating the "$@"construct:
"Assigning the arguments to a regular variable (as in args="$@") mashes all the arguments together just like "$*" does."
There seems to be something special about the "$@" construct, almost as if the resulting value that gets iterated over is a different type from other variables. Or maybe the resulting intermediary value has alternatively separated (not space separated) values or something.
In Python I could check this using type(@) or repr(@) but I haven't found a bash equivalent that preserves the internal separators of "$@" or whatever makes it behave specially when iterated over.
Perhaps it is simply not possible to duplicate the "$@" construct. If anyone knows, I would love to hear an explanation.
I'm not looking for a solution to a specific problem. (I already know how to work around problems caused by the above, although the workarounds are clumsy IMO.)
Rather I'd like to fully understand why it can't be duplicated, or what method could be used to duplicate it, which could help me better understand how bash works.
I could go digging through the bash source code, but I'd prefer not to have to, as that would be quite the rabbit hole to go down.
Thanks for any feedback. :)