How do I enter a literal tab character in a bash shell?

74

11

For example, I wanted to use the sort utility with the -t option to specify tab separators, but

sort -t "\t"

doesn't work.

Mark

Posted 2011-11-28T16:04:02.520

Reputation: 873

Answers

87

Don't use double quotes.

sort -t $'\t'

Or I think Ctrl V inserts a Tab??

Edit:

http://www.gnu.org/s/bash/manual/html_node/ANSI_002dC-Quoting.html#ANSI_002dC-Quoting

surfasb

Posted 2011-11-28T16:04:02.520

Reputation: 21 453

Doesn't tab insert a tab? – RedGrittyBrick – 2011-11-28T16:32:06.793

4@RedGrittyBrick Tab completes. – Daniel Beck – 2011-11-28T16:48:36.903

14Control-V alone won't work; Control-V + Tab will. I like the $'...' trick, though; now I've learned something new. :-) – L2G – 2011-11-28T17:04:45.757

I've always learned it as $' '. It allows you to enter a string, but also have escaped characters. Double quoting literally prints \t If you leave off the quotes you get a tab character. – surfasb – 2011-11-28T17:09:48.813

62

Try Control-v, then Tab. If you see the cursor tab over to the right, it worked.

According to the comment by Mark you can also try Control-v and then Control-i.

L2G

Posted 2011-11-28T16:04:02.520

Reputation: 806

why does the ctrl-v then tab and ctrl-v then ctrl-i work? what part of the bash man page describes why? – Trevor Boyd Smith – 2019-01-04T13:38:09.683

When I do this, I get a real tab (i.e. indentation). – Daniel Beck – 2011-11-28T16:49:22.830

2Oops. You're right. But it is entering the tab character, not doing command-line completion (which is what bash normally does with a tab). I tried sort -t " " (with the literal tab as described above) and it worked for me. – L2G – 2011-11-28T16:57:12.843

Yep, that's what I meant by indentation. Didn't know a better term. – Daniel Beck – 2011-11-28T17:13:46.547

1

Ctrl-v, Ctrl-i will also work (I found this answer here). Also, I think a Ctrl-q, Ctrl-v, Tab will work. Thanks L2G!

– Mark – 2011-11-28T17:20:51.240

BTW, I would love to accept both answers, but since I think surfasb's solution is more readable, I accepted hers. I like yours, too, though, so voted it up. Thanks! – Mark – 2011-11-28T17:23:54.183