Authors Top

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

Quoting in bash is really simple. However, sometimes, it becomes challenging when we add some restrictions. One of such restrictions is escaping a single quote within a single quote itself.

In this tutorial, we’ll discuss some of the ways to achieve this.

2. Using the Dollar ($) Symbol

In bash, strings starting with the dollar ($) symbol are treated specially. We can leverage this property to escape a single quote:

$ echo $'Problems aren\'t stop signs, they are guidelines'
Problems aren't stop signs, they are guidelines

Note that there’s a dollar ($) symbol at the beginning of the string.

3. Using the Escape Sequence

One more approach is to escape a single quote itself. Let’s divide the given string into three parts:

'Problems aren' + \' + 't stop signs, they are guidelines'

In the above example, the plus (+) symbol represents the concatenation operation. Let’s remove the plus symbol and space characters to achieve the desired result:

$ echo 'Problems aren'\''t stop signs, they are guidelines'
Problems aren't stop signs, they are guidelines

4. Using a Combination of Single and Double Quotes

Similar to the previous example, we can use a combination of single and double quotes:

"Problems aren" + "'" + "t stop signs, they are guidelines"

Let’s remove the plus symbol and space characters:

$ echo "Problems aren""'""t stop signs, they are guidelines"
Problems aren't stop signs, they are guidelines

5. Conclusion

In this article, we discussed three practical examples of using a single quote within a single quote itself. In the first example, we used the dollar ($) symbol. The second example shows the usage of multiple single quotes. The last example uses a combination of single and double quotes to achieve the desired result.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!