如何使用bash脚本回显一个或多个制表符? 当我运行这段代码时

res='       'x # res = "\t\tx"
echo '['$res']' # expect [\t\tx]

我明白了

res=[ x] # that is [<space>x]

当前回答

如果你想在脚本中使用echo "a\tb",你可以这样运行脚本:

# sh -e myscript.sh

或者,您也可以将执行权限赋予myscript.sh,然后运行脚本。

# chmod +x myscript.sh
# ./myscript.sh

其他回答

echo -e ' \t '

将回显'space制表符换行符' (-e表示'启用反斜杠转义'):

$ echo -e ' \t ' | hexdump -C
00000000  20 09 20 0a                                       | . .|

使用echo打印变量值是一个常见的Bash陷阱。 参考链接:

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

你需要为echo使用-e标志

echo -e "\t\t x"

你也可以试试:

echo Hello$'\t'world.

把你的字符串放在双引号之间:

echo "[$res]"