Escape an apostrophe in a sed command
How to get an apostrophe into a sed command: shell quoting options, portability tradeoffs, curly-quote conversion, and what survives copy and paste.
An apostrophe has no special meaning to sed. The problem comes from the shell. Most sed scripts are wrapped in single quotes, and in POSIX shells a single-quoted string cannot contain a single quote, not even one preceded by a backslash. Escaping an apostrophe in sed therefore means choosing a quoting method that passes the character through the shell to sed intact. A related task is swapping the straight apostrophe for a typographic one, or swapping it back. That task changes the stored characters in the text, so it raises separate questions about readability and compatibility.
Short recommendation
For a one-off command, the close-escape-reopen idiom (''') is the most portable choice because it works in any POSIX shell with any sed. Double quotes are easier to read and are fine when the script contains no dollar signs, backticks or backslashes. For long or reused scripts, put the sed commands in a file and run them with -f, which takes the shell out of the problem entirely. The main tradeoff is that the most portable inline methods are the hardest to read, and the most readable ones depend on a particular shell or sed implementation. Keep plain straight apostrophes in code, configuration files, shell commands and data that other programs will search or parse. Converting them to typographic apostrophes is only worthwhile in prose meant for human readers.
What the shell sees and what sed sees
The shell removes the outer quotes before sed runs. In the command sed 's/it'''s/it is/', the shell reads three adjacent pieces: the quoted string s/it, a backslash-escaped apostrophe outside any quotes, and the quoted string s/it is/. It joins them into one argument, so sed receives s/it's/it is/. Every working method follows the same principle: the shell must end up handing sed a plain apostrophe. Each example below prints "it is here".
# 1. Close the single-quoted string, insert an escaped apostrophe, reopen
printf '%s\n' "it's here" | sed 's/it'\''s/it is/'
# 2. Double quotes: the shell still interprets $, backticks and backslashes
printf '%s\n' "it's here" | sed "s/it's/it is/"
# 3. ANSI-C quoting (bash, zsh, ksh; not guaranteed in every /bin/sh)
printf '%s\n' "it's here" | sed $'s/it\'s/it is/'
# 4. Hex escape handled by sed itself (GNU sed extension)
printf '%s\n' "it's here" | sed 's/it\x27s/it is/'
# 5. Script file: the apostrophe needs no shell quoting inside it
printf '%s\n' "s/it's/it is/" > fix.sed
printf '%s\n' "it's here" | sed -f fix.sed
Each method carries its own risk. With double quotes, a regex anchor such as $ or a backslash escape must be written so the shell leaves it alone, usually as $ and \. In interactive bash, an exclamation mark inside double quotes can also trigger history expansion. ANSI-C quoting ($'...') reads cleanly but fails in shells that do not support it, so it is a poor fit for scripts that run under a minimal /bin/sh. The \x27 escape is documented for GNU sed. Other implementations, including the BSD-derived sed shipped with macOS, may not accept it. A script file avoids all shell quoting but adds a separate file to keep track of.
| Method | Portability | Readability | Main risk |
| Close-escape-reopen (''') | Any POSIX shell, any sed | Low | Hard to read and easy to mistype |
| Double quotes | Any POSIX shell, any sed | High | Shell expands $, backticks and backslashes |
| ANSI-C quoting ($'...') | bash, zsh, ksh | Medium | Not available in every /bin/sh |
| Hex escape (\x27) | GNU sed | Medium | Not portable to other sed implementations |
| Script file (-f) | Any sed | High | Extra file to maintain |
Converting straight apostrophes to typographic ones, and back
Some readers searching for this topic want to replace apostrophes rather than match them. The straight apostrophe is U+0027. In typeset prose it is usually replaced with the right single quotation mark, U+2019 (’). Some orthographies instead use the modifier letter apostrophe, U+02BC, for an apostrophe that functions as a letter. A sed substitution rewrites the bytes stored in the file. That differs from a font or CSS setting, which only changes how a straight apostrophe is drawn on screen and leaves the underlying character as U+0027. No reversal or mirroring is involved: the character order and the direction of the text stay the same.
# Straight to typographic (U+0027 -> U+2019)
sed "s/'/’/g" draft.txt
# Typographic back to straight (U+2019 -> U+0027)
sed "s/’/'/g" draft.txt
A global replacement does not know context. Converting every straight apostrophe also turns opening single quotes into closing ones, changes foot and minute marks (as in 5'), and corrupts any code, commands or markup quoted inside the text. Limit the substitution to the lines or patterns that actually contain prose, or review the changes before accepting them. Also run sed in a UTF-8 locale; the locale command shows the current setting. A literal ’ in a pattern still matches as a byte sequence in other locales. A bracket expression such as [’‘], however, is treated as a set of individual bytes outside a UTF-8 locale, and replacing those bytes can damage the text.
Will the result survive copying and sharing?
Commands are fragile when copied. Word processors, some web pages and some chat tools automatically turn straight quotes into curly ones. A sed command pasted from such a source contains ’ where the shell expects ', so the shell treats it as an ordinary character and the command typically fails with a quoting error from the shell or a parse error from sed. When a pasted command fails for no obvious reason, check its quote characters first. A plain-text editor, or retyping the quotes, avoids the problem.
Converted text also behaves differently in other tools. A search for it's does not match it’s unless the search tool normalizes apostrophes. Some tools do, but you should not assume one does without checking it. Formats such as CSV and JSON can hold ’ inside string values, but wherever an apostrophe is part of the syntax it must remain straight. The file must also be read with the same encoding it was written in. If ’ shows up as ’, a UTF-8 file is being read as Windows-1252.
No documentation or tests for specific screen readers, browsers or messaging apps were supplied for this review. Both apostrophe forms are standard Unicode characters, but how a particular assistive technology announces them may depend on its punctuation settings, and that has not been verified here. If the audience depends on assistive technology, or the text will be searched, keep a plain straight-apostrophe version or test in the actual destination.
A safe workflow
- Run the command on a sample without -i first and read the output.
- Compare the original and the result with diff before overwriting anything.
- When editing in place, use -i.bak, which keeps a backup and works in both GNU sed and BSD/macOS sed. A bare -i behaves differently between the two.
- Restrict substitutions to an address or pattern instead of the whole file when the file mixes prose and code.
- Afterward, count the remaining straight or typographic apostrophes, for example with grep -c, to confirm the change did what you intended.