views:

3675

answers:

5

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]
+5  A: 
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                                       | . .|
Johannes Weiß
Thanks Johannes. You helped me find this solution : res='\t\t'x; echo -e '['$res']'
kalyanji
@Johannes Weiß: Do you happen to know, why `echo -e` does not work from Makefiles?
dma_k
+1  A: 

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

echo -e "\t\t x"
Learning
+2  A: 

Put your string between double quotes:

echo "[$res]"
kmkaplan
+1  A: 

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

http://wooledge.org:8000/BashPitfalls#echo.24foo

Anonymous
+1  A: 

You can also try:

echo Hello$'\t'world.
jbatista