For example, using this script:
#!/bin/bash
for a in $@
do
echo $a
done
And running:./script "x y" z
returns:
x
y
z
and not:
x y
z
Why is that?
And how would I pass string arguments with spaces to bash?
I use Bash 4.3.33
.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityFor example, using this script:
#!/bin/bash
for a in $@
do
echo $a
done
And running:./script "x y" z
returns:
x
y
z
and not:
x y
z
Why is that?
And how would I pass string arguments with spaces to bash?
I use Bash 4.3.33
.
Quote $@
:
#!/bin/bash
for a in "$@"
do
echo "$a"
done
Output:
x y z
bash
, it's enough to say for a
. It will assume that you mean in "$@"
.
Mar 23, 2015 at 19:25
T=$@ for a in $T do echo "$a" done
does not work.
Mar 23, 2015 at 23:01
t=("$@")
, and then reference it as "${t[@]}"
to recover the original argument list intact. Note that all of the double-quotes I used are necessary.
Mar 24, 2015 at 1:16
Note that in when dealing with $@
you can simplify it to
for a
do
echo "$a"
done
If `in WORDS ...;' is not present, then `in "$@"' is assumed.