Quotes, quotes, quotes: A primer for the command line
In Bash programming, there are a lot of ways to get input into programs. In particular, there are a slew of different quoting methods you should understand. This article provides a quick reference of the difference between using No quotes, Double Quotes, Single Quotes, and Backticks
Standard shell scripts assumes arguments are space delimited. You can iterate over elements in this way:
for i in Hi how are you; do echo $i; done Hi how are you
This is why it is a problem to have spaces in your file names. For instance,
$ ls with spaces.txt $ cat with spaces.txt cat: with: No such file or directory cat: spaces.txt: No such file or directory
Here I naively typed with spaces.txt thinking the cat program could handle it. Instead, cat saw two arguments: with, and spaces.txt. In order to handle this, you can either escape the space,
$ cat with\ spaces.txt
or use the double quotes method. (Note that if you use tab autocompletion, the backslash escape will be added automatically)
Double quotes can be used when you want to group multiple space delimited words together as a single argument. For instance
for i in "Hi how" "are you"; do echo $i; done Hi how are you
In the previous example, I could do
$ cat "with spaces.txt"
and the filename would be passed as a single unit to cat.
An important thing to note is that shell variables are expanded within double quotes.
name=Frank; echo "Hello $name" Hello Frank
This is crucial to understand. It also allows you to solve problems caused by having spaces in file names, especially when combined with the * globbing behavior of the shell. For instance, let’s say we wanted to iterate over all the text files in a directory and do something to them.
$ ls with spaces.txt withoutspaces.txt $ for i in *.txt; do cat $i; done cat: with: No such file or directory cat: spaces.txt: No such file or directory # Surround the $i with quotes and our space problem is solved. $ for i in *.txt; do cat "$i"; done
(Yes I know iterating over and calling cat on each argument is silly, as cat can accept a list of files (e.g. *.txt). But it illustrates the point that commands will be confused by spaces in the name and should use double quotes to handle the problem).
Single quotes are also good when you need to embed single quotes in a string (you do not need to escape them)
$ echo "'Single quotes'" 'Single quotes' $ echo "\"Escaped quotes\"" "Escaped quotes"
Double quotes are my default while I’m working in the terminal.
Single quotes act just like double quotes except that the text inside of them is interpreted literally; in other words, the shell does not attempt to do any more expansion or substitution. For instance,
$ name=Frank; echo 'Hello $name' Hello $name
This can save you some backslash escaping your normally would have to do.
Use it when:
- You need double quotes embedded in your string
$ echo '"How are you doing?", she said' "How are you doing?", she said
- You do not need any literal single quotes in your string (it’s very difficult to get single quotes/apostrophe literals to appear in such a string)
Back ticks (“, the key to the left of the 1 and above the Tab key on a standard US keyboard), allow you to substitute in the output of another command. For instance:
$ current_dir=`pwd` $ echo $current_dir /Users/nicholasdunn/Desktop/Scripts [/sourecode] This can be combined with the double quotes, but will be treated as literal characters in the single quotes: echo "`pwd`" /Users/nicholasdunn/Desktop/Scripts $ echo '`pwd`' `pwd`
You want to capture the results of another command, usually for purposes of assigning a variable.
Hopefully this brief tour through the different types of quotes in bash has been useful.