Reputation: 421
Take this small example:
#!/bin/bash -x
myvar="\"Hello Stackoverflow\""
ping $myvar
The result of this is that any commands (ping is just for example here) will automatically see the whitespace between 'Hello' and 'Stackoverflow' as a newline or carridge return.
# ./test.sh
+ myvar='"Hello Stakeoverflow"'
+ ping '"Hello' 'Stakeoverflow"'
ping: unknown host "Hello
Is there anyway I can overide this?
Ignore the use of ping, I understand you can't ping words, its just helpful to draw the example on how variables are processed when supplied as an argument to a shell command and so I need an error to show that.
Upvotes: 1
Views: 477
Reputation: 295291
The internal quotes do you no good -- don't try to use them. In the case of myvar="\"foo\""
, the outer quotes are syntax, but the inner quotes are data. Quotes that are data aren't used by the shell for string-splitting purposes, and they don't turn back into syntax later (unless you use eval
, which introduces a host of new problems, many of them security-impacting); all they do is add data that your actual command doesn't want or need.
You need syntactic quotes around your expansion to prevent string-splitting and glob expansion. Compare these four examples (only the first of which is correct):
do_something_with() {
printf '<%s> ' "$@"
echo
}
myvar="Hello Stackoverflow"
do_something_with "$myvar" # output: <Hello Stackoverflow>
do_something_with $myvar # output: <Hello> <Stackoverflow>
myvar2="\"Hello Stackoverflow\""
do_something_with $myvar2 # output: <"Hello> <Stackoverflow">
do_something_with "$myvar2" # output: <"Hello Stackoverflow">
If you want to store something which should be interpreted as multiple arguments in a variable, the proper thing to use is an array:
my_array=( Hello "Stack Overflow" )
do_something_with "${my_array[@]}" # output: <Hello> <Stack Overflow>
See also http://mywiki.wooledge.org/BashFAQ/050
Upvotes: 3