When working with strings containing single quotes, often a fundamental task, escaping single quotes becomes essential to maintain the high integrity of Bash scripts. This escaping skill allows you to deal with a variety of data within a string including single quotes in a literal format. In this article, I’ll explore how you can escape single quotes in Bash using different cases.
Key Takeaways
- Learning about the escape quoting of single quotes in Bash.
- Understanding different cases of escaping single quotes.
Context of Escaping Single Quotes in Bash
Escaping is necessary for those cases when you feel the need to include literal single quotes within strings like regular expression patterns, configuration files, single-quoted strings etc.
How Does the Problem Arise?
In Bash scripting, every time you use a single quote, the shell interprets all characters including special characters inside the quote as literal values. But there arises a syntax error when you include a single quote within a single-quoted string as Bash interprets the outer single quotes as the delimiter of the string. So, how to encase single quotes in a single-quoted string?
Here, escaping is the solution that comes to work around the above problem. You can easily solve the issue by using a special syntax discussed in detail in Case 1 of the following section.
3 Different Cases to Escape Single Quotes in Bash
Here, I am going to demonstrate three different cases to escape single quotes in Bash:
Case 1: Using a Backslash Character (\) to Escape Single Quotes in Bash
In Bash scripting, one effective way to escape a single quote enclosed within a single-quoted string is to use a backslash character (\). Here are the steps to follow:
Steps to Follow >
-
Go to Ubuntu Terminal, open a script in the Nano text editor by running the following command:
nano backslash.sh
EXPLANATION- nano: A text editor.
- backslash.sh: This is a script. Here, I have named the script ‘backslash.sh’. You can name any of your choices.
- Now, write the following script inside the editor:Script (backslash.sh) >
#!/bin/bash #Displaying the output echo 'It'\''s a website called LinuxSimply & I'\''m escaping here.'
EXPLANATIONHere, in #!/bin/bash, ‘#!’ is called ‘Shebang’ or ‘Hashbang’. Then, in the line echo ‘It’\”s a website called LinuxSimply & I’\”m escaping here.’, 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 backslash.sh
EXPLANATION- chmod: Changes the permission of the files and directories.
- u+x: Adds the executable permission for the user.
- backslash.sh: The file which you want to make executable.
- Finally, run the script by the following command:
./backslash.sh
From the image, you can see that I have escaped the single quotes that are enclosed within a single-quoted string by using a backslash in the ‘\” format.
Case 2: Using Double Quotes (“) to Escape Single Quotes in Bash
Encase all the single quotes in a double-quoted string to directly escape the single quotes without using any symbol or character in Bash.
Script (doubleQuote.sh) >
#!/bin/bash
#Displaying the output
echo "It's a website called LinuxSimply & I'm escaping here."
Here, in the line echo “It’s a website called LinuxSimply & I’m escaping here.”, the echo command displays the output.
Now, run the script by the following command:
./doubleQuote.sh
From the image, you can see that I have escaped the single quotes directly that are enclosed within a double-quoted string.
Case 3: Using Backslash (\) Inside ($’…’) Syntax to Escape Single Quotes in Bash
Generally, Bash treats the strings specially that are prefixed with the ‘$’ symbol. So, you can easily escape single quotes using the ANSI-C quoted string $’…’. And you must put a backslash (\) before every single quote inside the syntax $’…’.
Script (dollar.sh) >
#!/bin/bash
#Displaying the output
echo $'It\'s a website called LinuxSimply & I\'m escaping here.'
Here, in the line echo $’It\’s a website called LinuxSimply & I\’m escaping here.’, the echo command displays the output.
Now, run the script by the following command:
./dollar.sh
The above image resembles the escape quoting of single quotes using a backslash where the single quotes are enclosed within the syntax $’…’.
Conclusion
To wrap up, escaping single quotes is such a crucial technique that enables you to confidently handle several scenarios of a proper interpretation of single quotes.
People Also Ask
Related Articles
- How to Escape Double Quotes in Bash? [3 Easy 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