Escaping is a great concept when you are dealing with double quotes and want to avoid unwanted interpretations and errors within a Bash script. In this article, I’ll illustrate how to escape double quotes in Bash and include them in a literal format within a string by using several effective cases. Let’s explore!
Key Takeaways
- Learning about the escape quoting of single quotes in Bash.
- Understanding different cases of escaping single quotes.
Why Escaping Double Quotes in Bash?
In Bash scripting, double quotes are generally used to expand variables, interpret commands and particular special characters, etc. But what to do when you don’t want to perform any of the mentioned tasks? Escaping is the only solution in this regard that includes all the characters within a string as literal values. You can also prevent the execution of variable and command substitution by escaping double quotes in Bash.
3 Different Cases to Escape Double Quotes in Bash
The following section is the demonstration of three different cases to escape double quotes in Bash:
Case 1: Using a Backslash Character (\) to Escape Double Quotes in Bash
To escape a double quote encased in a double-quoted string, use a backslash character (\) before each double quote inside. Follow the given steps to do so:
Steps to Follow >
➊ Go to Ubuntu Terminal, open a script in the Nano text editor by running the following command:
nano backslashChar.sh
- nano: A text editor.
- backslashChar.sh: This is a script. Here, I have named the script ‘backslashChar.sh’. You can name any of your choices.
➋ Now, write the following script inside the editor:
Script (backslashChar.sh) >
#!/bin/bash
#Displaying the output
echo "I am escaping the word \"LinuxSimply\"."
Here, in #!/bin/bash, ‘#!’ is called ‘Shebang’ or ‘Hashbang’. Then, in the line echo “I am escaping the word \”LinuxSimply\”.”, the echo command displays the output.
➌ Then, press CTRL+S to save the file & press CTRL+X to exit.
➍ After that, use the command below to make the script executable:
chmod u+x backslashChar.sh
- chmod: Changes the permission of the files and directories.
- u+x: Adds the executable permission for the user.
- backslashChar.sh: The file which you want to make executable.
➎ Finally, run the script by the following command:
./backslashChar.sh
From the image, you can see that I have escaped the double quotes enclosed within a double-quoted string by using a backslash before them.
Case 2: Using Single Quotes (‘) to Escape Double Quotes in Bash
If you want to escape double quotes directly without using any characters, then simply include the double quotes in a single-quoted string.
Script (singleQuote.sh) >
#!/bin/bash
#Displaying the output
echo 'I am escaping the word "LinuxSimply".'
Here, in the line echo ‘I am escaping the word “LinuxSimply”.’, the echo command displays the output.
Now, run the script by the following command:
./singleQuote.sh
From the image, you can see that I have directly escaped the double quotes that are enclosed within a single-quoted string without using any characters.
Case 3: Using ($’…’) Syntax to Escape Double Quotes in Bash
Another way to escape double quotes easily in Bash is to use the ANSI-C quoted string $’…’. Just include the double quotes within the string in the following manner:
Script (dollarsign.sh) >
#!/bin/bash
#Displaying the output
echo $'I am escaping the word "LinuxSimply".'
Here, in the line echo $’I am escaping the word “LinuxSimply”.’, the echo command displays the output.
Now, run the script by the following command:
./dollarsign.sh
The above image resembles the escape quoting of double quotes enclosed within the syntax $’…’.
Conclusion
Summing up the whole article, master the proper quoting technique of escaping double quotes to prevent unintended bugs and make the Bash interpret strings within your scripts accurately.
People Also Ask
Related Articles
- How to Escape Single Quotes in Bash? [3 Interactive Cases]
- 4 Ways to Remove or Strip Quotes from String in Bash With Cases
<< Go Back to Escape Quotes in Bash | Bash Quotes | Bash Scripting Tutorial