FUNDAMENTALS A Complete Guide for Beginners
Escape quotes are the special tools in Bash for ensuring the correct handling of single quotes, double quotes, and special characters of various forms. In this article, I’ll explain how you can employ escape quotes in Bash diversely to handle a wide range of data interpretation and create error-free scripts. So, let’s explore!
Key Takeaways
- Learning about the escape characters in Bash.
- Exercising different cases of escaping quotes in Bash.
What Are Escape Quotes in Bash?
Escape quotes are a type of special quoting characters in Bash that you can use to indicate certain characters in a literal format within a command or string. Generally, the escape character in Bash is symbolized by a backslash (\) that maintains the characters’ literal structure.
Actually, these escape quotes are used especially when you want to prevent them from showing their actual meaning in Bash. Just like, if you want to escape a variable prefixed with the $ symbol, then Bash will ignore the $ symbol and interpret only the variable name instead of its value.
5 Cases to Employ Escape Quotes in Bash
The following section illustrates how you can escape quotes in different cases in Bash:
Case 1: Escaping Single Quotes Enclosed in Single Quotes in Bash
There is no direct way to escape the single quotes (‘) whenever you want to encase them in a single-quoted string. In this effort, you need to exit the single-quoted string in a temporary manner, put a backslash (\), and then reopen the single-quoted string. Here’s an example:
Steps to Follow >
➊ Go to Ubuntu Terminal, open a script in the Nano text editor by running the following command:
nano single1.sh
- nano: A text editor.
- single1.sh: This is a script. Here, I have named the script ‘single1.sh’. You can name any of your choices.
➋ Now, write the following script inside the editor:
Script (single1.sh) >
#!/bin/bash
#Displaying the output
echo 'It'\''s LinuxSimply!'
Here, in #!/bin/bash, ‘#!’ is called ‘Shebang’ or ‘Hashbang’. Then, in the line echo ‘It’\”s 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 single1.sh
- chmod: Changes the permission of the files and directories.
- u+x: Adds the executable permission for the user.
- single1.sh: The file which you want to make executable.
➎ Finally, run the script by the following command:
./single1.sh
From the image, you can see that I have escaped a single quote enclosed in the single-quoted string ‘It’s LinuxSimply!‘ using the syntax ‘\”.
Case 2: Escaping Single Quotes Enclosed in Double Quotes Including Variable Interpretation
If you want to include a single quote (‘) as literal characters within a double-quoted string, you can do so directly allowing the variable expansion in the following manner:
Script (single2.sh) >
#!/bin/bash
#Declaring variable
variable="website"
#Displaying the output
echo "Yes, it's the $variable, 'LinuxSimply'."
Here, in the line variable=’website’, I have declared a variable called ‘variable’. Next, in the line echo “Yes, it’s the $variable, ‘LinuxSimply’.”, the echo command displays the output with the variable expansion.
Now, run the script by the following command:
./single2.sh
In the above image, I have escaped single quotes enclosed in double quotes including the variable expansion.
Case 3: Escaping Double Quotes Enclosed in Single Quotes Without Variable Interpretation
It’s necessary to escape double quotes (“) when you want to include these as literal characters within a single quoted string. However, no variable expansion will occur in this effort as the whole string is enclosed in single quotes. Let’s see an example!
Script (double1.sh) >
#!/bin/bash
#Declaring variable
website="LinuxSimply"
#Displaying the output
echo 'Hello, "X"! This is the $website.'
Here, in the line website=”LinuxSimply”, I have declared a variable called ‘website’. Next, in the line echo ‘Hello, “X”! This is the $website.’, the echo command displays the output without expanding the value of the variable.
Now, run the script by the following command:
./double1.sh
The above image illustrates the escape quoting of the double quotes within single quotes without allowing any variable interpretation.
Case 4: Escaping Double Quotes Enclosed in Double Quotes
Whenever you want to show double quotes as literal characters within a double-quoted string, you must escape the double quotes inside by putting a backslash (\) just before it. A similar execution is given below:
Script (double2.sh) >
#!/bin/bash
#Displaying the output
echo "This is the website \"LinuxSimply\"."
Here, in the line echo “This is the website \”LinuxSimply\”.”, the echo command displays the output along with the variable substitution.
Now, run the script by the following command:
./double2.sh
The above image depicts the escape quoting of double quotes that are enclosed within a double-quoted string using the syntax \”.
Case 5: Escape Quoting of Backslash (\) in Bash
If you want to insert a backslash (\) as a literal character within a string, then you need to escape it by including another backslash before it.
Script (backslash.sh) >
#!/bin/bash
echo "Here is one backslash: \\"
Here, in the line echo “Here is one backslash: \\”, the echo command displays the output.
Now, run the script by the following command:
./backslash.sh
The above snapshot dictates the escape quoting of backslash (\) as a literal character within a string.
Conclusion
To conclude, escaping quotes is such a crucial aspect in Bash through which you can manage huge data formats and maintain the commands’ accuracy and scripts’ integrity.
People Also Ask
Related Articles
<< Go Back to Bash Quotes | Bash Scripting Tutorial