Linked Questions
338 questions linked to/from When is double-quoting necessary?
378 votes
6 answers
421k 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 ...
282 votes
3 answers
57k 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 ...
213 votes
3 answers
205k 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 ...
131 votes
8 answers
308k views
Bash: Assign output of pipe to a variable
I am trying to get the output of a pipe into a variable. I tried the following things: echo foo | myvar=$(</dev/stdin) echo foo | myvar=$(cat) echo foo | myvar=$(tee) But $myvar is empty. I don’t ...
223 votes
3 answers
111k views
$VAR vs ${VAR} and to quote or not to quote
I can write VAR=$VAR1 VAR=${VAR1} VAR="$VAR1" VAR="${VAR1}" the end result to me all seems about the same. Why should I write one or the other? are any of these not portable/POSIX?
136 votes
4 answers
55k 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 ...
42 votes
5 answers
84k views
Replace \n by a newline in sed portably
I'm so confused with GNU sed, POSIX sed, and BSD sed's myriad of options. How do I replace literal \n with the newline character using these three sed types?
68 votes
2 answers
25k views
Documentation on LESS_TERMCAP_* variables?
I see these pretty colors in manpages viewed with less when setting a bunch of these variables. What do they mean, where is the documentation? Is this interpreted by less or termcap? screenshot
73 votes
1 answer
14k 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 ...
29 votes
7 answers
47k views
How can I use a variable as a case condition?
I am trying to use a variable consisting of different strings separated with a | as a case statement test. For example: string="\"foo\"|\"bar\"" read choice case $choice in $string) echo "...
32 votes
3 answers
79k views
Syntax error near unexpected token `fi`
I don't necessarily want the answer but if someone could point me to some literature or examples. I would like to figure it out. When I run the script I receive an error: Syntax error near ...
29 votes
6 answers
29k views
Bash function that accepts input from parameter or pipe
I want to write the following bash function in a way that it can accept its input from either an argument or a pipe: b64decode() { echo "$1" | base64 --decode; echo } Desired usage: $ ...
26 votes
5 answers
5k views
Why use double quotes in a [[ ]] test?
Let's say we have 2 integers in a bash script: value1=5 value2=3 Then why do we need to use double quotes in case of a test ? For example: if [[ "$value1" -eq "$value2" ]] Why not just use the ...
12 votes
6 answers
2k views
Too many shebang (script declaration) lines --- any way to reduce their amount?
I have a project comprised of about 20 small .sh files. I name these "small" because generally, no file has more than 20 lines of code. I took a modular approach because thus I'm loyal to the Unix ...
14 votes
3 answers
29k views
How to pass all arguments of a function along to another command?
Hello I have this in my ~/.bash_profile export GOPATH="$HOME/go_projects" export GOBIN="$GOPATH/bin" program(){ $GOBIN/program $1 } so I'm able to do program "-p ...