Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Word splitting happens even with double quotes

Tags:

bash

According to http://tldp.org/LDP/abs/html/quotingvar.html

Use double quotes to prevent word splitting. An argument enclosed in double quotes presents itself as a single word, even if it contains whitespace separators.

However, I have

0> /bin/bash --version
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
[...]

1> cat inspect.sh 
#!/bin/bash
echo "first argument is $1"
echo "second argument is $2"

2> cat test.sh 
#!/bin/bash
set -x
./inspect.sh "hello $@"

3> ./test.sh alice bob
+ ./inspect.sh 'hello alice' bob
first argument is hello alice
second argument is bob

4> ./test.sh "alice bob"
+ ./inspect.sh 'hello alice bob'
first argument is hello alice bob
second argument is 

I'm wondering why 3> and 4> have different results? How to modify test.sh to let 3> have the same output as 4>?

like image 439
Xiao Jia Avatar asked Jan 10 '23 03:01

Xiao Jia


1 Answers

The answer is that $@ is special.

From the Bash Reference Manual section 3.4.2 Special Parameters:

@

Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$@" is equivalent to "$1" "$2" …. If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters, "$@" and $@ expand to nothing (i.e., they are removed).

Specifically:

  • When the expansion occurs within double quotes, each parameter expands to a separate word.

and

  • If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word.

Which combined gets you "hello " merged to the first argument and "" merged to the final argument but each argument expanded to its own word.

If you want to expand all the arguments to one word then you want to use $*.

*

Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. That is, "$*" is equivalent to "$1c$2c…", where c is the first character of the value of the IFS variable. If IFS is unset, the parameters are separated by spaces. If IFS is null, the parameters are joined without intervening separators.

like image 109
Etan Reisner Avatar answered Jan 19 '23 06:01

Etan Reisner