There are only a few special characters involved in working with character strings on the command line or in a script on Linux: the single quote, the double quote and the backslash. But the rules aren’t as obvious as one might think. In this post, we’ll look at the easy and the somewhat tricky uses of these special characters.
The echo command doesn’t require any variety of quote characters much of the time. You can echo a phrase like this without bothering with quotes of any kind.
$ echo Enter your name: Enter your name:
If you want to use quotes, there’s no problem with that. Either of these commands will work:
$ echo “Enter your name:” $ echo ‘Enter your name:’
In simple cases like these, it makes no difference whether you use single or double quotes. In fact, you can even use both single and double quotes in a single echo command if you are so inclined. While the quotes don’t add anything, they don’t get in the way either.
$ echo “Make today” a very ‘good day!’
Make today a very good day!
Enclosing one set of quotes inside the other, however, makes a noticeable difference. The inner quotes will be retained. Here are two examples:
$ echo "today is a 'good' day" today is a 'good' day
$ echo 'today is a "good" day' today is a "good" day
When you need quotes
There are times when quotes are required. One example is when you want to use an apostrophe in a phrase. After all, an apostrophe and a single quote are the same on your keyboard, so you don’t want to confuse bash!
$ echo Please don't eat the daisies >
In the command shown above, bash assumes that you still intend to end the quote that you started and prompts for you to continue. To avoid having bash think you’re starting but not ending a quote, enclose the text inside double quotes and it will do what you intended:
$ echo “Please don't eat the daisies” Please don’t eat the daisies
Of course, the other thing that you can do is use a backslash character. This tells bash that you don’t want the apostrophe to be interpreted, but just displayed.
$ echo Please don’t each the daisies Please don’t eat the daisies
You can also use the backslash character to have a word or phrase displayed as a quote:
$ echo She likes to refer to herself as ”Mamacita” She likes to refer to herself as “Mamacita”
Quotes and variables
Whenever you want to assign more than one string to a variable, you need to use quotes. Otherwise, bash will assume that any strings after the first are commands that you are trying to run and will not complete the assignment of the value.
$ when=next week bash: week: command not found... $ echo $when
Enclosing the string that you are assigning in quotes solves the problem. No errors involved!
$ when='last week' $ echo $when last week
The quotes that you use also make a difference when using variables. Use double quotes when you want to include a variable in the output.
$ today=Saturday echo "Today is $today" Today is Saturday echo 'Today is $today' Today is $today
The single quotes display the name of the variable rather than showing its value.
Comparing strings using the if command
The choice of quotes also makes a difference when you are comparing strings in an if command. If a variable has a multi-string value, quotes are required, and the choice of quotes also makes a difference. This works:
$ final=’the end’ $ if [ “$final” == ‘the end’ ]; then > echo Goodbye for now > else > echo huh? > fi Goodbye for now
Single quotes won’t work for the comparison:
$ final='the end' $ if [ '$final' == 'the end' ]; then > echo Goodbye for now > else > echo huh? > fi huh?
Why the difference? Because, as suggested in an earlier example, using ‘$final’ in single quotes compares the string ‘the end’ to the literal string ‘$final‘, not to the value assigned to the variable. Echoing the variable in single quotes or passing its output to the octal dump (od -bc) command shows what the if command above is viewing:
$ echo '$final'
$ echo '$final' | od -bc 0000000 044 146 151 156 141 154 012 $ f i n a l n 0000007
Having both single and double quotes along with the backslash character available allows you to work effectively with strings in bash. There are just a few rules that you need to get it right every time.