89

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
  • 1,023
  • 1
  • 7
  • 6
  • Why won't '\011' work? Tried it on my Linux box and for some reason it doen't work. – Bing Bang Jun 03 '20 at 22:55
  • I tried \t and it doesn't work, neither does \n, Is there a csh switch to turn off escaped characters? If there is, I've never heard of it. – Bing Bang Jun 03 '20 at 23:26

4 Answers4

109

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
  • 22,452
  • 5
  • 52
  • 77
  • Doesn't tab insert a tab? – RedGrittyBrick Nov 28 '11 at 16:32
  • 6
    @RedGrittyBrick Tab completes. – Daniel Beck Nov 28 '11 at 16:48
  • 17
    Control-V alone won't work; Control-V + Tab will. I like the `$'...'` trick, though; now *I've* learned something new. :-) – L2G Nov 28 '11 at 17:04
  • 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 Nov 28 '11 at 17:09
  • won't work in dash – CervEd Feb 09 '23 at 12:59
  • good to know, thanks :) -- I also discovered that: echo $'\t' is different from echo "$'\t'" -- that seems a bit weird, since normally $something works the same, with or without douiblequotes.... – Rop Jun 02 '23 at 13:46
72

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.

Mateusz Piotrowski
  • 4,016
  • 4
  • 28
  • 53
L2G
  • 906
  • 5
  • 10
  • 1
    When I do this, I get a real tab (i.e. indentation). – Daniel Beck Nov 28 '11 at 16:49
  • 3
    Oops. 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 Nov 28 '11 at 16:57
  • Yep, that's what I meant by indentation. Didn't know a better term. – Daniel Beck Nov 28 '11 at 17:13
  • 1
    Ctrl-v, Ctrl-i will also work (I found this answer [here](http://ask.metafilter.com/18273/tab-character-in-linux)). Also, I think a Ctrl-q, Ctrl-v, Tab will work. Thanks L2G! – Mark Nov 28 '11 at 17:20
  • 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 Nov 28 '11 at 17:23
  • 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 Jan 04 '19 at 13:38
  • what if my clipboard is full... and ctrl-q is a terrible idea, closes application – Nimitz14 Apr 11 '21 at 09:19
1

To put tab:

  • First press Ctr + v and

  • Then press tab key.


Example

Using above mentioned step adding tab enter image description here

Saving in file enter image description here

Final Output enter image description here


Reference: https://linuxjourney.com/lesson/cut-command

Nikhil
  • 121
  • 5
0

You can also use printf:

sort -t "$(printf "\t")"

Not like $'\t', printf use double quotes which allow you to use environment variables, like below:

char="\t" # any source just plain text

sort -t "$(printf "$char")"

Single quote is static and not flexible, though it's simple, you can choose based on your requirement.

James Yang
  • 101
  • 1