Echo tab characters in bash script

kalyanji picture kalyanji · Feb 8, 2009 · Viewed 472.8k times · Source

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]

Answer

Johannes Weiss picture Johannes Weiss · Feb 8, 2009
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                                       | . .|