Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

1. Overview

Quoting in bash is really simple. However, sometimes, it becomes challenging when we add some restrictions. One such restriction 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. Conclusion

In this article, we discussed a few practical examples of using a single quote within a single quote itself. In the first example, we used the dollar ($) symbol, while the second example shows the usage of multiple single quotes.