Echo tab characters in bash script

后端 未结 10 2107
予麋鹿
予麋鹿 2020-12-07 08:09

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         


        
相关标签:
10条回答
  • 2020-12-07 08:36

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

    echo -e "\t\t x"
    
    0 讨论(0)
  • 2020-12-07 08:38
    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                                       | . .|
    
    0 讨论(0)
  • 2020-12-07 08:38

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

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

    0 讨论(0)
  • 2020-12-07 08:42

    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'
    
    0 讨论(0)
提交回复
热议问题