Recently, while I was writing a bash script to process some tab delimited data, I faced the problem of setting control characters (such as tab) as field delimiters in tools like cut or sort.
The “obvious” way would be to write something like:
cut -d'\t' -f2
but this does not work as cut complains that the delimiter must be a single character.
(Note: the tab character is the default delimiter in cut, but I am just using cut as an example. The same observations hold for sort, paste and other similar tools).
What works is writing:
cut -d' ' -f2
with a single tab character between the single quotes.
I find this way very brittle: it is hard to read and the tab character could easily be mistaken for spaces.
My preferred method is to use the shell
cut -d$'\t' -f2
This way it is clear that the delimiter is a single tab character. The syntax works of course for other control sequences. It works also for setting the IFS variable.
$'' syntax is bash specific (I do not know if it works in other shells).