For developers, and everyone else who wants to automate processes on Unix or Linux systems, knowing How to Escape Single Quotes in Bash is essential. In a bash script, you may occasionally need to utilize a single quotation (‘) inside a string. However, if this isn’t handled correctly, it could cause problems because of how bash perceives single quotes. We’ll look into how to escape single quotes in bash scripts in this article.
Understanding the Problem:
In bash scripting, single quotes (‘) are used to preserve the literal value of all characters within the quotes. Inside single quotes, every special character is preserved in its literal form, with no exceptions. For instance:
will trigger a syntax error since bash will consider the second single quotation to be the string’s end.
Therefore, how can we add a single quote to a string that has single quotes surrounding it already? That is the purpose of running away.
The Solution: Escaping Single Quotes:
Sadly, unlike double quotes(“), where you can escape with a backslash(\), single quotes inside a single-quoted string in bash cannot be done. When a backslash appears inside a single-quoted string, bash interprets it as a backslash rather than an escape character.
But if you stop the single-quoted string, add an escaped single quote, and then begin a new single-quoted string, you can get the desired result. Although this could appear complicated, the following example will make things clear:
“It” is the first single-quoted string in this example. Next comes \’, an escaped single quotation, and then the second single-quoted string, “s a sunny day.” The following is the output of this command: It’s a sunny day.
You can alternatively write it like this to make it easier to read:
This command will also output: It’s a sunny day.
Working with Variables
When you need to incorporate single quotes in a string that is kept in a variable, you may encounter problems. Let’s take an example where you have the following variable:
And you want to print: It’s a $day. You might be tempted to try:
Nevertheless, since variables aren’t expanded within single quotes, this won’t function as intended. Rather, you’ll actually receive It’s a $day.
As variables are enlarged within double quotes, you can circumvent this by ending the single-quoted string once more, using double quotes for the variable, and then beginning a new single-quoted string. This is how you do it:
This command will output: It’s a sunny day.
Using Backslash Character:
Use a backslash character (
\) to escape a single quote in Bash. We use this approach if the string is enclosed within the single quotes.
Use Backslash Character
echo ‘I’\‘‘m escaping a single quote. Again, I’\‘‘m doing it.’
I‘m escaping a single quote. Again, I’m doing it.
\' must be wrapped with single quotes in the above example.
While it might seem complex at first, escaping single quotes in bash scripts is quite straightforward once you understand how it works. You can’t escape single quotes within a single-quoted string, but you can achieve the desired effect by ending the single-quoted string, adding an escaped single quote, and then starting a new single-quoted string. And remember, if you need to include variables in your strings, you’ll have to use double quotes for the variable part.
Get more information about
Using the =~ Operator in Bash