Echo tab characters in bash script

BashTabsEchoSpaces

Bash Problem Overview


How do I echo one or more tab characters using a bash script? When I run this code

res='		'x # res = "\t\tx"
echo '['$res']' # expect [\t\tx]

I get this

res=[ x] # that is [<space>x]

Bash Solutions


Solution 1 - Bash

echo -e ' \t '

will echo 'space tab space newline' (-e means 'enable interpretation of backslash escapes'):

$ echo -e ' \t ' | hexdump -C
00000000  20 09 20 0a                                       | . .|

Solution 2 - Bash

Use printf, not echo.

There are multiple different versions of the echo command. There's /bin/echo (which may or may not be the GNU Coreutils version, depending on the system), and the echo command is built into most shells. Different versions have different ways (or no way) to specify or disable escapes for control characters.

printf, on the other hand, has much less variation. It can exist as a command, typically /bin/printf, and it's built into some shells (bash and zsh have it, tcsh and ksh don't), but the various versions are much more similar to each other than the different versions of echo are. And you don't have to remember command-line options (with a few exceptions; GNU Coreutils printf accepts --version and --help, and the built-in bash printf accepts -v var to store the output in a variable).

For your example:

res='           'x # res = "\t\tx"
printf '%s\n' "[$res]"

And now it's time for me to admit that echo will work just as well for the example you're asking about; you just need to put double quotes around the argument:

echo "[$res]"

as kmkaplan wrote (two and a half years ago, I just noticed!). The problem with your original commands:

res='           'x # res = "\t\tx"
echo '['$res']' # expect [\t\tx]

isn't with echo; it's that the shell replaced the tab with a space before echo ever saw it.

echo is fine for simple output, like echo hello world, but you should use printf whenever you want to do something more complex. You can get echo to work, but the resulting code is likely to fail when you run it with a different echo implementation or a different shell.

Solution 3 - Bash

You can also try:

echo Hello$'\t'world.

Solution 4 - Bash

Put your string between double quotes:

echo "[$res]"

Solution 5 - Bash

you need to use -e flag for echo then you can

echo -e "\t\t x"

Solution 6 - Bash

From the bash man page:

> Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard.

So you can do this:

echo $'hello\tworld'

Solution 7 - Bash

Use the verbatim keystroke, ^V (CTRL+V, C-v, whatever).

When you type ^V into the terminal (or in most Unix editors), the following character is taken verbatim. You can use this to type a literal tab character inside a string you are echoing.

Something like the following works:

echo "^V<tab>"     # CTRL+V, TAB

Bash docs (q.v., "quoted-insert")

> quoted-insert (C-q, C-v) Add the next character that you type to the line verbatim. This is how to insert key sequences like C-q, for example.

side note: according to this, ALT+TAB should do the same thing, but we've all bound that sequence to window switching so we can't use it

> tab-insert (M-TAB) Insert a tab character.

--

Note: you can use this strategy with all sorts of unusual characters. Like a carriage return:

echo "^V^M"        # CTRL+V, CTRL+M

This is because carriage return is ASCII 13, and M is the 13th letter of the alphabet, so when you type ^M, you get the 13th ASCII character. You can see it in action using ls^M, at an empty prompt, which will insert a carriage return, causing the prompt to act just like you hit return. When these characters are normally interpreted, verbatim gets you get the literal character.

Solution 8 - Bash

Using echo to print values of variables is a common Bash pitfall. Reference link:

http://mywiki.wooledge.org/BashPitfalls#echo_.24foo

Solution 9 - Bash

If you want to use echo "a\tb" in a script, you run the script as:

# sh -e myscript.sh

Alternatively, you can give to myscript.sh the execution permission, and then run the script.

# chmod +x myscript.sh
# ./myscript.sh

Solution 10 - Bash

res="\t\tx"
echo -e "[${res}]"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkalyanjiView Question on Stackoverflow
Solution 1 - BashJohannes WeissView Answer on Stackoverflow
Solution 2 - BashKeith ThompsonView Answer on Stackoverflow
Solution 3 - BashjbatistaView Answer on Stackoverflow
Solution 4 - BashkmkaplanView Answer on Stackoverflow
Solution 5 - BashLearningView Answer on Stackoverflow
Solution 6 - BashesorianoView Answer on Stackoverflow
Solution 7 - Bashrich remerView Answer on Stackoverflow
Solution 8 - BashAnonymousView Answer on Stackoverflow
Solution 9 - BashATCView Answer on Stackoverflow
Solution 10 - BashyanghaognView Answer on Stackoverflow