Echo tab characters in bash script


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]
11/23/2014 8:54:52 PM

Accepted Answer

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                                       | . .|
2/16/2016 7:22:22 AM

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow