在 bash 脚本中回显制表符
如何使用 bash 脚本回显一个或多个制表符? 当我运行这段代码时,
res=' 'x # res = "\t\tx"
echo '['$res']' # expect [\t\tx]
我得到了这个
res=[ x] # that is [<space>x]
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]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
将回显“空格制表符空格换行符”(
-e
表示“启用反斜杠转义的解释”):will echo 'space tab space newline' (
-e
means 'enable interpretation of backslash escapes'):使用
printf
,而不是echo
。echo
命令有多个不同版本。 有/bin/echo
(可能是也可能不是 GNU Coreutils 版本,具体取决于系统),并且echo
命令内置于大多数 shell 中。 不同的版本有不同的方法(或没有方法)来指定或禁用控制字符的转义。另一方面,
printf
的变化要少得多。 它可以作为命令存在,通常为/bin/printf
,并且它内置于某些 shell 中(bash 和 zsh 有,tcsh 和 ksh 没有),但各种版本更类似于不同版本的echo
之间的区别。 而且您不必记住命令行选项(有一些例外;GNU Coreutils printf 接受--version
和--help
,以及内置的 bash printf 接受-v var
将输出存储在变量中)。对于您的示例:
现在是时候让我承认
echo
对于您所询问的示例同样有效; 你只需要在参数周围加上双引号:正如 kmkaplan 所写(两年半前,我刚刚注意到!)。 原始命令的问题:
不在于
echo
; 这是因为 shell 在echo
看到它之前就用空格替换了制表符。echo
适合简单输出,例如echo hello world
,但只要您想做更复杂的事情,就应该使用printf
。 您可以让echo
工作,但是当您使用不同的echo
实现或不同的shell运行它时,生成的代码可能会失败。Use
printf
, notecho
.There are multiple different versions of the
echo
command. There's/bin/echo
(which may or may not be the GNU Coreutils version, depending on the system), and theecho
command is built into most shells. Different versions have different ways (or no way) to specify or disable escapes for control characters.printf
, on the other hand, has much less variation. It can exist as a command, typically/bin/printf
, and it's built into some shells (bash and zsh have it, tcsh and ksh don't), but the various versions are much more similar to each other than the different versions ofecho
are. And you don't have to remember command-line options (with a few exceptions; GNU Coreutils printf accepts--version
and--help
, and the built-in bash printf accepts-v var
to store the output in a variable).For your example:
And now it's time for me to admit that
echo
will work just as well for the example you're asking about; you just need to put double quotes around the argument:as kmkaplan wrote (two and a half years ago, I just noticed!). The problem with your original commands:
isn't with
echo
; it's that the shell replaced the tab with a space beforeecho
ever saw it.echo
is fine for simple output, likeecho hello world
, but you should useprintf
whenever you want to do something more complex. You can getecho
to work, but the resulting code is likely to fail when you run it with a differentecho
implementation or a different shell.您还可以尝试:
You can also try:
将字符串放在双引号之间:
Put your string between double quotes:
你需要使用 -e 标志来回显然后你可以
you need to use -e flag for echo then you can
从 bash 手册页:
所以你可以这样做:
From the bash man page:
So you can do this:
使用逐字击键
^V
(CTRL+V
、Cv
、随便)。当您在终端(或大多数 Unix 编辑器)中键入
^V
时,将逐字记录以下字符。 您可以使用它在您正在回显的字符串中键入文字制表符。类似以下内容的作品:
Bash 文档 (qv ,“引用插入”)
旁注:根据此,
ALT+TAB
应该做同样的事情,但我们都将该序列绑定到窗口切换,因此我们不能使用它--
注意:您可以对各种不寻常的角色使用此策略。 像回车符:
这是因为回车符是 ASCII 13,而 M 是字母表中的第 13 个字母,所以当你输入
^M
时,你得到的是第 13 个 ASCII特点。 您可以在空提示符处使用 ls^M 来查看它的运行情况,这将插入一个回车符,使提示符的行为就像您按回车键一样。 当这些字符被正常解释时,逐字会让您得到字面字符。Use the verbatim keystroke,
^V
(CTRL+V
,C-v
, whatever).When you type
^V
into the terminal (or in most Unix editors), the following character is taken verbatim. You can use this to type a literal tab character inside a string you are echoing.Something like the following works:
Bash docs (q.v., "quoted-insert")
side note: according to this,
ALT+TAB
should do the same thing, but we've all bound that sequence to window switching so we can't use it--
Note: you can use this strategy with all sorts of unusual characters. Like a carriage return:
This is because carriage return is ASCII 13, and M is the 13th letter of the alphabet, so when you type
^M
, you get the 13th ASCII character. You can see it in action usingls^M
, at an empty prompt, which will insert a carriage return, causing the prompt to act just like you hit return. When these characters are normally interpreted, verbatim gets you get the literal character.使用 echo 打印变量值是一个常见的 Bash 陷阱。
参考链接:
http://mywiki.wooledge.org/BashPitfalls#echo_.24foo
Using echo to print values of variables is a common Bash pitfall.
Reference link:
http://mywiki.wooledge.org/BashPitfalls#echo_.24foo
如果您想在脚本中使用
echo "a\tb"
,请按以下方式运行脚本:或者,您可以向 myscript.sh 授予执行权限,然后运行该脚本。
If you want to use
echo "a\tb"
in a script, you run the script as:Alternatively, you can give to myscript.sh the execution permission, and then run the script.