Open In App

Bash Script – Quotes and its types

Last Updated : 24 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Quoting in bash scripting is a process to make variables or any other container for data to be expanded to the literal value inside any string, quoting is also used for other operations as well. There are several types of quoting techniques in a bash script. In this article, we will see the different types of quoting in Bash scripting.

Types of Quotes in BASH

  • Single Quotes
  • Double Quotes 
  • Back Quotes

Single Quotes:

By using single quotes the string is parsed as it is without any expansion of characters inside the quotes. So, if we have a variable inside the string the value won’t be expanded instead the characters will be parsed as it is. 

Let’s say we have a variable called name and we try to echo its value inside a single quote, we need to use $ to access the variable value. But the value isn’t printed and the command is echoed as it is. 

#!/usr/bin/env bash

name="Jim"

echo 'This is $name, a developer'

Output:

So, single quotes are not great if you wanted to use the variable expansion and other related stuff. You can close the quotes before the variable and then open again just like concatenation but it’s not great for multiple variables. 

Double Quotes:

To expand the variable value inside a string we use the double-quotes. Using double quotes we can expand the literal value of the variable by just prefixing the variable name with $ as said earlier for accessing the value with the variable name. 

#!/usr/bin/env bash

name="Jim"

echo "This is $name, a developer"

Output:

So, as we can see the variable value was expanded and the command worked as expected. The variable value can be anything like integers or strings or any characters that are not expandable further.

Not only variable expansion, the other characters that are special when embedded inside double-quotes.  

#!/usr/bin/env bash

name="Jim"
age=23
languages=("python" "javascript" "java" "C" "Rust")

echo "This is $name, a $age old developer.
${languages[@]}
`pwd`
\" Hello WO\orld \" "

Output:

So, this is how we can perform variable expansion in a double quote, we can even escape characters in double quotes by using a backslash. The backslash operator allows parsing the next character as it is without considering it as a special character for expansion or as a wildcard operator.

We have used the variable name and age so as to demonstrate the expansion of different types of data variables inside the double-quotes. We have even used operators to print the entire array namely the ${array_name[@]}. We have also added backquotes (“) to use a command inside double-quotes. And finally, the backslash character to escape those characters in this case the double quotes (“”). Escaping a character is to parse the character in such a way that it is just treated as an ordinary character without allowing its operations as the special character. Some characters you need to escape a\might be $, #, double quotes, single quotes, etc. 

Escaping Characters in double quotes

To escape characters we can use \ before the character to parse it as it is. For example, you need to print the $ symbol you need type \$ to work as desired inside the double quotes. We have used \” to escape the double quotes and by that, we have used double quotes inside the double quotes. So, to parse the character without its special operations to perform we need to use \ to simply print it.

Back Quotes(Backticks):

In the previous section, we have used the backquotes to use the commands inside the double-quotes. The back quotes allow us to execute the commands inside a shell script, Not only they can be used inside the double quotes they can be used independently in the script.

#!/usr/bin/env bash

echo `python --version` 
echo `cat wh.txt`
echo `date`

Output:

As we can see here we can execute commands from the script. These are the commands which can be executed inside the command prompt, yes you can use certain commands like cat, pwd, etc as it is in the script but not all commands can be used as it is in the script. For executing those commands we can use the back quotes(` `). These can be also be nested in double-quotes as we saw earlier in the double quotes example.  

So these are the quoting techniques we use in the shell script to expand variables, execute commands and perform and execute other operations/scripts from a shell script. The quoting can be used for programming certain commands and executing/expanding them as and when required by embedding the commands in the appropriate quotes.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads