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
you need to use -e flag for echo then you can
echo -e "\t\t x"
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 | . .|
Using echo to print values of variables is a common Bash pitfall. Reference link:
http://mywiki.wooledge.org/BashPitfalls#echo_.24foo
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'