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

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

sort -t "\t"

doesn't work.

2

3 Answers

Don't use double quotes.

sort -t $'\t'

Or I think Ctrl V inserts a Tab??

Edit:

4

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.

7

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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like