Using single and double quotes on Linux is easy if you know a few essential rules. Credit: Thinkstock 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. Echoing text 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' $final $ echo '$final' | od -bc 0000000 044 146 151 156 141 154 012 $ f i n a l n 0000007 Wrap-up 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. Related content how-to How to examine files on Linux Linux provides very useful options for viewing file attributes, such as owners and permissions, as well as file content. By Sandra Henry Stocker Oct 24, 2024 6 mins Linux how-to 8 easy ways to reuse commands on Linux Typing the same command again and again can become tiresome. Here are a number of ways you can make repeating commands – or repeating commands but with some changes – a lot easier than you might expect. By Sandra Henry-Stocker Oct 15, 2024 5 mins Linux news SUSE Edge upgrade targets Kubernetes and Linux at the edge SUSE Edge 3.1 includes a new stack validation framework and an image builder tool that are aimed at improving the scalability and manageability of complex Kubernetes and Linux edge-computing deployments. By Sean Michael Kerner Oct 15, 2024 6 mins Edge Computing Linux Network Management Software how-to Lesser-known xargs command is a versatile time saver Boost your Linux command line options and simplify your work with xargs, a handy tool for a number of data manipulation tasks. By Sandra Henry Stocker Oct 11, 2024 6 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe