Howdy!
I'm making a script that has to deal with filenames with whitespaces in them and I encoutered an issue. I made a small simple script to demonstrate my question:
foo
The output of:
Now, a behaviour that I actually want is case [1], when I can iterate over the argument list and have each argument recognized correctly (with whitespaces preserved). But I noticed that there's no way to achieve that if I also want to store $@ in a variable. I tried all of the 4 possible combinations of quotations (cases [2]-[5]) and none of them worked. I tried to grab some info from sh man page (did a search for 'quotes' stuff), but couldn't find anything related to my question there.
Could you please explain why none of [2]-[5] act like [1] and how can I have both: 1) output like in [1] and 2) have $@ stored in a variable.
Thanks.
I'm making a script that has to deal with filenames with whitespaces in them and I encoutered an issue. I made a small simple script to demonstrate my question:
foo
sh:
#!/bin/sh
for arg in "$@"; do
echo "[1] Arg: $arg"
done
########
args="$@"
for arg in "$args"; do
echo "[2] Arg: $arg"
done
########
args="$@"
for arg in $args; do
echo "[3] Arg: $arg"
done
########
args=$@
for arg in "$args"; do
echo "[4] Arg: $arg"
done
########
args=$@
for arg in $args; do
echo "[5] Arg: $arg"
done
The output of:
./foo arg1\ space arg2\ space
Code:
[1] Arg: arg1 space
[1] Arg: arg2 space
[2] Arg: arg1 space arg2 space
[3] Arg: arg1
[3] Arg: space
[3] Arg: arg2
[3] Arg: space
[4] Arg: arg1 space arg2 space
[5] Arg: arg1
[5] Arg: space
[5] Arg: arg2
[5] Arg: space
Now, a behaviour that I actually want is case [1], when I can iterate over the argument list and have each argument recognized correctly (with whitespaces preserved). But I noticed that there's no way to achieve that if I also want to store $@ in a variable. I tried all of the 4 possible combinations of quotations (cases [2]-[5]) and none of them worked. I tried to grab some info from sh man page (did a search for 'quotes' stuff), but couldn't find anything related to my question there.
Could you please explain why none of [2]-[5] act like [1] and how can I have both: 1) output like in [1] and 2) have $@ stored in a variable.
Thanks.