All Questions
119
questions
375
votes
6
answers
409k
views
Why does my shell script choke on whitespace or other special characters?
… or an introductory guide to robust filename handling and other string passing in shell scripts.
I wrote a shell script which works well most of the time. But it chokes on some inputs (e.g. on some ...
181
votes
1
answer
78k
views
When is double-quoting necessary?
The old advice used to be to double-quote any expression involving a $VARIABLE, at least if one wanted it to be interpreted by the shell as one single item, otherwise, any spaces in the content of $...
275
votes
4
answers
53k
views
Security implications of forgetting to quote a variable in bash/POSIX shells
If you've been following unix.stackexchange.com for a while, you
should hopefully know by now that leaving a variable
unquoted in list context (as in echo $var) in Bourne/POSIX
shells (zsh being the ...
197
votes
6
answers
299k
views
How can we run a command stored in a variable?
$ ls -l /tmp/test/my\ dir/
total 0
I was wondering why the following ways to run the above command fail or succeed?
$ abc='ls -l "/tmp/test/my dir"'
$ $abc
ls: cannot access '"/tmp/test/my': No ...
73
votes
1
answer
13k
views
What is the difference between the "...", '...', $'...', and $"..." quotes in the shell?
Sometimes I see shell scripts use all of these different ways of quoting some text: "...", '...', $'...', and $"...". Why are there so many different kinds of quote being used?
Do ...
129
votes
4
answers
52k
views
Why does parameter expansion with spaces without quotes work inside double brackets "[[" but not inside single brackets "["?
I'm confused with using single or double brackets. Look at this code:
dir="/home/mazimi/VirtualBox VMs"
if [[ -d ${dir} ]]; then
echo "yep"
fi
It works perfectly although the string contains a ...
155
votes
5
answers
470k
views
How to escape quotes in the bash shell?
I'm having trouble with escaping characters in bash. I'd like to escape single and double quotes while running a command under a different user. For the purposes of this question let's say I want to ...
205
votes
3
answers
191k
views
Quoting within $(command substitution) in Bash
In my Bash environment I use variables containing spaces, and I use these variables within command substitution.
What is the correct way to quote my variables? And how should I do it if these are ...
33
votes
4
answers
14k
views
Quoting in ssh $host $FOO and ssh $host "sudo su user -c $FOO" type constructs
I often end up issuing complex commands over ssh; these commands involve piping to awk or perl one-lines, and as a result contain single quotes and $'s. I have neither been able to figure out a hard ...
10
votes
2
answers
5k
views
Difference between ' and " on command line (bash)? [duplicate]
I used to use '' and "" (single and double-quotes) interchangeably on the command line, but I recently noticed that '$HOME/some/dir' is not expanded, while "$HOME/some/dir" is. I searched around a ...
26
votes
6
answers
125k
views
Is there any way to print value inside variable inside single quote?
Consider I've set variable site and needs to be printed by echo or printf, but If I use single quote to write something and want to use variable then how?
Example:
$ site=unix.stackexchange.com
$ ...
27
votes
2
answers
66k
views
Single quote within double quotes and the Bash reference manual
In section 3.1.2.3 titled Double Quotes, the Bash manual says:
Enclosing characters in double quotes (‘"’) preserves the literal
value of all characters within the quotes, with the exception of ...
16
votes
3
answers
13k
views
Why does the exclamation mark `!` sometimes upset bash?
I realize that ! has special significance on the commandline in the context of the commandline history, but aside from that, in a runing script the exclamation mark can sometimes cause a parsing error....
188
votes
7
answers
179k
views
Can't use exclamation mark (!) in bash?
I'm trying to use the curl command to access a http url with a exclamation mark (!) in its path. e.g:
curl -v "http://example.org/!287s87asdjh2/somepath/someresource"
the console replies with bash: ....
20
votes
2
answers
12k
views
How Can I Expand A Tilde ~ As Part Of A Variable?
When I open up a bash prompt and type:
$ set -o xtrace
$ x='~/someDirectory'
+ x='~/someDirectory'
$ echo $x
+ echo '~/someDirectory'
~/someDirectory
I was hoping that the 5th line above would have ...