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

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

我明白了

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

当前回答

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

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

其他回答

你需要为echo使用-e标志

echo -e "\t\t x"
res="\t\tx"
echo -e "[${res}]"

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

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

从bash手册页:

形式为$'string'的单词被特殊处理。单词扩展为字符串,反斜杠转义字符替换为ANSI C标准指定的字符。

所以你可以这样做:

echo $'hello\tworld'

你也可以试试:

echo Hello$'\t'world.