How to print an apostrophe with awk

Why an apostrophe breaks an awk one-liner, and reliable ways to print one: the \047 octal escape, a -v variable, shell quote splicing, a script file or printf.

Printing an apostrophe (') from awk sounds trivial, but it is one of the most common snags in shell one-liners. The difficulty does not come from awk. It comes from the shell quoting that surrounds the awk program. This guide explains the cause, gives several working ways to output a straight apostrophe, shows how to confirm the exact character you produced, and covers the typographic (curly) apostrophe for readers who need typeset punctuation.

Why the apostrophe breaks your awk command

An awk program typed on the command line is usually wrapped in single quotes. This stops the shell from expanding awk field references such as $1 before awk sees them. In POSIX-style shells such as sh, bash and zsh, a single-quoted string cannot contain a single quote. A backslash does not help, because backslashes have no special meaning inside single quotes. The first apostrophe in your text therefore ends the quoted program early.

awk 'BEGIN { print "It's done" }'

In this broken example, the shell ends the quoted string at It, and the rest of the line is parsed as ordinary shell text. Depending on what follows, the shell shows a continuation prompt while it waits for a closing quote, or awk receives a truncated program and reports a syntax error. Every fix below avoids placing a raw apostrophe inside the single-quoted program.

Method 1: Use the octal escape \047 (best for one-liners)

Awk string literals support octal escape sequences of the form \ddd, as defined by the POSIX awk specification. The apostrophe is character 39 in ASCII, which is 047 in octal. The shell passes the backslash sequence through unchanged inside single quotes, and awk converts it into an apostrophe when the program runs.

awk 'BEGIN { print "It\047s done" }'

Expected output: It's done. The BEGIN block runs before any input is read, so this command needs no input file. That makes it convenient for testing.

The same escape works when you wrap fields in apostrophes, for example when building quoted values for another program. The input file and its contents in the following snippet are illustrative.

$ cat names.txt
alice
bob
$ awk '{ print "\047" $1 "\047" }' names.txt
'alice'
'bob'

You may also see the hexadecimal form \x27. Gawk and some other implementations accept it, but POSIX does not define it. Some implementations, including older versions of gawk, keep reading hexadecimal digits after \x. If the next letter in your text is a valid hex digit (a–f), it can be swallowed into the escape. The octal form always uses exactly three digits, so \047 is the safer and more portable choice.

Method 2: Pass the apostrophe in a variable with -v

The -v option assigns an awk variable before the program starts. If you put the apostrophe inside shell double quotes, the shell treats it as a literal character. Awk string concatenation then joins the variable with the surrounding text. This keeps the program readable when it needs many apostrophes.

awk -v q="'" 'BEGIN { print "It" q "s done" }'

Expected output: It's done. Adjacent strings and variables in awk are concatenated, so "It" q "s done" produces a single string. Choose a variable name that does not clash with names already used in your program.

Method 3: Close the quote, add an escaped apostrophe, reopen

This is a shell technique rather than an awk feature. You end the single-quoted string with ', supply a backslash-escaped apostrophe ' outside any quotes, and then start a new single-quoted string with '. The shell joins adjacent pieces into one argument, so awk receives an ordinary apostrophe.

awk 'BEGIN { print "It'\''s done" }'

Expected output: It's done. The four-character sequence ''' works in POSIX-style shells, but it gets hard to read when it appears several times. For that reason, Methods 1 and 2 are usually easier to maintain.

Method 4: Put the program in a script file

When awk reads its program from a file with -f, the shell never parses the program text. The apostrophe can be written as is. This is the cleanest option for longer programs.

# greet.awk
BEGIN { print "It's done" }
awk -f greet.awk

Expected output: It's done. Inside the file, apostrophes only need care if they appear in a regular expression or string where awk itself gives them meaning. Awk does not treat the apostrophe as special in either place.

Method 5: Print the character by its code with printf

The awk printf statement supports the %c format. When the matching argument is a number, %c prints the character with that code. Passing 39 therefore outputs an apostrophe.

awk 'BEGIN { printf "It%cs done\n", 39 }'

Expected output: It's done. Unlike print, printf does not add a newline automatically, so the format string ends with \n.

An approach to avoid: double-quoting the whole program

You could wrap the entire awk program in double quotes so that the apostrophe becomes legal. However, the shell then expands $1, $0 and similar references and interprets backslashes before awk sees the program. Every dollar sign and every inner double quote would need escaping. The resulting errors are easy to miss, because the command may still run and produce wrong output.

Quick comparison

Method | Fragment | Best for | Caveat
Octal escape | "It\047s" | One-liners, portable scripts | Less readable at a glance
-v variable | -v q="'" ... "It" q "s" | Programs that need many apostrophes | Adds a variable name
Quote splicing | 'It'''s' | Quick fixes in the shell | Hard to read when repeated
Script file | awk -f file.awk | Longer programs | Requires a separate file
printf %c | printf "%c", 39 | Building characters from codes | Needs an explicit newline

Check that you printed the right character

An apostrophe on screen can be a straight ASCII apostrophe or a look-alike, such as a right single quotation mark or a prime symbol. To check exactly which character awk produced, pipe the output into od, which displays each byte.

awk 'BEGIN { print "It\047s done" }' | od -An -tx1

For the text It's done followed by a newline, the expected bytes are 49 74 27 73 20 64 6f 6e 65 0a. The byte 27 (hexadecimal) is the straight apostrophe. If you see e2 80 99 in that position instead, the output contains a curly apostrophe encoded in UTF-8. Use od -c instead if you prefer to see the characters themselves rather than hex values.

Printing a typographic (curly) apostrophe

Typeset text often uses the right single quotation mark (’, Unicode U+2019) as an apostrophe. This character is not the shell's quote character, so it causes no quoting problems. In a UTF-8 environment you can type it directly inside the awk string. If you want the program to stay pure ASCII, you can write its three UTF-8 bytes as octal escapes instead.

awk 'BEGIN { print "It\342\200\231s done" }'

Expected output: It’s done. This assumes that the terminal or the receiving file uses UTF-8. In any other encoding, those three bytes will appear as unrelated characters.

Converting straight apostrophes in a file without losing the original

To replace every straight apostrophe in a file with a curly one, use gsub. Write the result to a new file. Standard awk does not edit files in place, and redirecting output to the same file you are reading empties that file before awk reads it.

awk '{ gsub(/\047/, "\342\200\231"); print }' notes.txt > notes-curly.txt
diff notes.txt notes-curly.txt

Review the diff before you replace the original file. A blanket replacement also changes straight single quotes that should stay unchanged. Examples include single quotation marks used around phrases, which may need a left quotation mark (‘) at the start, and quotes inside code, shell commands or configuration values. Feet and minutes notation such as 5'10 is another case. If the file mixes prose with any of these, limit the replacement to specific patterns, such as an apostrophe between two letters, instead of matching every apostrophe.

Scope and limits of this guide

  • The commands assume a POSIX-style shell such as sh, bash or zsh. Windows cmd.exe and PowerShell use different quoting rules that are not covered here.
  • The expected outputs follow documented POSIX awk behavior. They are not captured from a test run on a particular awk implementation or version, so use the od check above to confirm results on your system.
  • The hexadecimal escape \x27 and in-place editing are implementation-specific extensions and are not available in every awk.