Linux and Bash commands with ANSI-C escaping

17 Feb 2022

Let’s assume you have a simple command such as this:

Bash
1
sort -k1,2 -t'|' infile > outfile

…where -t is the field separator (and -k is the sort key).

You can specify field separators using a different notation, instead of the literal character:

Bash
1
sort -k1,2 -t$'\x01' infile > outfile

This example uses x01 as the field separator - which is the “start of heading” (SOH) control character.

See here for more details.

This is actually the ANSI-C quoting syntax.

Another example:

Bash
1
$'\042'

This is the octal code 042 representing a double quote (").

There are various commands which support this notation. Particularly useful if you need to handle control characters.