Bash script: Quotation explained with examples

Quotation on a Linux system can be a source of confusion at first. Single quotes ' and double quotes " are treated differently in Bash, and you will need to know the difference if you are writing a Bash script.

In this tutorial, you will learn the difference between single quotes and double quotes. You will also see how to escape quotes in your Bash script. Follow along with our examples below to quickly master quotation in Bash.

In this tutorial you will learn:

  • How to use double quotes " in Bash scripting
  • How to use single quotes ' in Bash scripting
  • What is the difference between single quotes and double quotes?
  • How to escape double quotes or single quotes
An example of using different kinds of quotation in a Bash script on Linux
An example of using different kinds of quotation in a Bash script on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Bash shell (installed by default)
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Bash script: Quotation explained with examples



DID YOU KNOW?
If you have mismatched quotes in your Bash script, you will encounter a unexpected end of file error. You can see our guide on Bash script: Unexpected end of file error for help troubleshooting those errors.
  1. Double quotes can be used to for strings that contain spaces in your Bash script. Take a look at the following example where we store a space inside of a variable.
    #!/bin/bash
    
    var="hello world"
    
    echo $var

    Since there is a space between the two words, it is necessary to wrap the whole string in quotes. We could have also used single quotes, although that would cause Bash to interpret the string more literally as you will see in the next example.

  2. Single quotes will cause Bash to interpret the string literally. The following example illustrates the principal difference between single quotes and double quotes in a Bash script.
    #!/bin/bash
    
    var="hello world"
    
    echo "$var"
    echo '$var'

    Here is the result when we execute the script:

    $ ./test.sh 
    hello world
    $var
    

    As you can see, the variable was expanded inside of the double quotes, but not inside of the single quotes. Note that it was not necessary to wrap "$var" in double quotes in this example, but it is generally a good idea to do so, as your script can run into errors if the variable contains spaces and you try to expand it without first wrapping it in double quotes.

  3. Things get a little trickier if we want to escape our quotes. Let’s look at an example where we try to make our hello world text get echoed with single quotes and double quotes.
    #!/bin/bash
    
    var="hello world"
    
    # echo the variable
    echo "$var"
    
    # echo literally
    echo '$var'
    
    # echo the variable in " "
    echo '"'"$var"'"'
    
    # echo the variable in ' '
    echo \'"$var"\'

    Here is the result when we execute the script:

    $ ./test.sh 
    hello world
    $var
    "hello world"
    'hello world'
    

    On line 12 of our script, we echoed the variable inside of double quotes. To achieve this, we wrap the double quotes inside of single quotes (so that they are interpreted literally). But, we also wrapped the variable in double quotes, as mentioned above this is best practice and will be required in some scripts you write. Line 15 is a bit easier to interpret. We simply use the backslash \ to escape our single quotes, but still wrap the variable in double quotes.



Closing Thoughts

In this tutorial, we saw how quotation works in a Bash script on Linux. The key takeaways are that variables are expanded inside of double quotes, but not inside of single quotes. Double quotes can be escaped using single quotes, and single quotes (as well as double quotes) can be escaped using backslashes. Now you will know which type of quotes to use in any situation.



Comments and Discussions
Linux Forum