Programming
Single Quotes
String Manipulation
Coding Tips
Escape Characters

How to escape single quotes within single quoted strings

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In POSIX shells and Bash, a single-quoted string is completely literal. That means a single quote cannot appear inside another single-quoted string directly; the usual solution is to end the quote, insert an escaped quote, and then start a new single-quoted segment.

Why a Single Quote Cannot Be Escaped Inside Single Quotes

Inside single quotes, the shell treats characters literally. Backslash does not escape a single quote there.

bash
# invalid shell syntax
printf '%s\n' 'Don\'t'

That fails because the shell sees the second single quote as the end of the string. The backslash is not special in that context.

This is the rule that matters most: once you are inside single quotes, there is no special escape sequence for another single quote.

The Standard Shell Pattern

To embed a literal single quote, close the quoted string, add an escaped quote outside the single quotes, and reopen the quoted string.

bash
printf '%s\n' 'Don'\''t stop'

The shell reads that as three pieces:

  • ''Don''
  • '\''
  • ''t stop''

Those pieces become one final string: Don't stop.

This looks odd at first, but it is the standard portable answer for shell quoting.

Use Double Quotes When They Make the Command Clearer

If the string does not need single-quote semantics, double quotes may be easier to read.

bash
printf '%s\n' "Don't stop"

Double quotes still allow parameter expansion, command substitution, and some backslash escapes, so they are not equivalent to single quotes. Use them when that behavior is acceptable or desirable.

Single quotes are still better when you want the content treated as literally as possible.

Build Shell-Safe Arguments Programmatically

When generating shell commands from another program, do not hand-roll quoting casually. A common portable shell-escaping pattern is to wrap the whole string in single quotes and replace every embedded single quote with the sequence ' \'' ' conceptually.

python
value = "it's ready"
escaped = "'" + value.replace("'", "'\\''") + "'"
print(escaped)

That produces a shell-safe single-quoted representation of the string. The important principle is that the embedded quote is handled by ending the single-quoted section, emitting a quoted literal quote, and reopening the single-quoted section.

Practical Example in a Shell Script

This comes up often when you need to pass text containing apostrophes to another command.

bash
name="O'Brien"
printf '%s\n' 'User: '"$name"

In many scripts, mixing single-quoted literal fragments with double-quoted variables is clearer than trying to force the whole command into one quoting style.

The real goal is not “use single quotes everywhere.” The goal is “produce the exact argument bytes you intend.”

Common Pitfalls

  • Trying to escape a single quote with backslash inside single quotes.
  • Forgetting that shell quoting rules differ between single and double quotes.
  • Writing long mixed-quote commands that are correct but impossible to maintain.
  • Generating shell commands manually in another language without using a disciplined escaping strategy.
  • Assuming the answer for shell quoting is the same as the answer for SQL, Python, or JavaScript string literals.

Summary

  • In shell syntax, a single quote cannot be escaped directly inside single quotes.
  • The portable pattern is 'foo'\''bar'.
  • Double quotes may be simpler when their interpolation behavior is acceptable.
  • Mixed quoting is often the clearest way to build real commands.
  • Focus on producing the exact argument the shell should pass, not on forcing one quoting style everywhere.

Course illustration
Course illustration

All Rights Reserved.