在 bash 脚本中回显制表符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/525872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Echo tab characters in bash script
提问by kalyanji
How do I echo one or more tab characters using a bash script? When I run this code
如何使用 bash 脚本回显一个或多个制表符?当我运行此代码时
res=' 'x # res = "\t\tx"
echo '['$res']' # expect [\t\tx]
I get this
我明白了
res=[ x] # that is [<space>x]
回答by Johannes Weiss
echo -e ' \t '
will echo 'space tab space newline' (-e
means 'enable interpretation of backslash escapes'):
将回显“空格制表符空格换行符”(-e
意思是“启用反斜杠转义解释”):
$ echo -e ' \t ' | hexdump -C
00000000 20 09 20 0a | . .|
回答by Keith Thompson
Use printf
, not echo
.
使用printf
,不是echo
。
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 the echo
command is built into most shells. Different versions have different ways (or no way) to specify or disable escapes for control characters.
该echo
命令有多个不同版本。有/bin/echo
(可能是也可能不是 GNU Coreutils 版本,取决于系统),并且该echo
命令内置在大多数 shell 中。不同的版本有不同的方式(或没有方式)来指定或禁用控制字符的转义。
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 of echo
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).
printf
另一方面,变化要小得多。它可以作为命令存在,通常是/bin/printf
,并且它内置在一些 shell 中(bash 和 zsh 有,tcsh 和 ksh 没有),但与不同版本的不同版本相比,各种版本彼此之间更相似echo
。而且您不必记住命令行选项(有一些例外;GNU Coreutils printf 接受--version
and --help
,而内置的 bash printf 接受-v var
将输出存储在变量中)。
For your example:
对于您的示例:
res=' 'x # res = "\t\tx"
printf '%s\n' "[$res]"
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:
现在是时候让我承认这echo
对于您所询问的示例同样有效;你只需要在参数周围加上双引号:
echo "[$res]"
as kmkaplan wrote (two and a half years ago, I just noticed!). The problem with your original commands:
正如 kmkaplan 所写(两年半前,我才注意到!)。原始命令的问题:
res=' 'x # res = "\t\tx"
echo '['$res']' # expect [\t\tx]
isn't with echo
; it's that the shell replaced the tab with a space before echo
ever saw it.
不是与echo
; 这是外壳在echo
看到它之前用空格替换了选项卡。
echo
is fine for simple output, like echo hello world
, but you should use printf
whenever you want to do something more complex. You canget echo
to work, but the resulting code is likely to fail when you run it with a different echo
implementation or a different shell.
echo
对于简单的输出很好,比如echo hello world
,但是printf
当你想要做一些更复杂的事情时你应该使用。您可以开始echo
工作,但是当您使用不同的echo
实现或不同的 shell运行它时,生成的代码可能会失败。
回答by kmkaplan
Put your string between doublequotes:
将您的字符串放在双引号之间:
echo "[$res]"
回答by jbatista
You can also try:
你也可以试试:
echo Hello$'\t'world.
回答by Learning
you need to use -e flag for echo then you can
你需要使用 -e 标志作为 echo 然后你可以
echo -e "\t\t x"
回答by esoriano
From the bash man page:
从 bash 手册页:
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.
$'string' 形式的词被特殊处理。单词扩展为字符串,并按照 ANSI C 标准的规定替换反斜杠转义字符。
So you can do this:
所以你可以这样做:
echo $'hello\tworld'
回答by rich remer
Use the verbatim keystroke, ^V
(CTRL+V
, C-v
, whatever).
使用逐字击键^V
( CTRL+V
, C-v
,随便)。
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.
当您^V
在终端(或在大多数 Unix 编辑器中)键入时,会逐字读取以下字符。您可以使用它在要回显的字符串中键入文字制表符。
Something like the following works:
类似以下的工作:
echo "^V<tab>" # CTRL+V, TAB
Bash docs(q.v., "quoted-insert")
Bash 文档(qv,“引用插入”)
quoted-insert (C-q, C-v) Add the next character that you type to the line verbatim. This is how to insert key sequences like C-q, for example.
quoted-insert (Cq, Cv) 将您键入的下一个字符逐字添加到行中。例如,这就是如何插入像 Cq 这样的键序列。
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
旁注:根据this,ALT+TAB
应该做同样的事情,但我们都将该序列绑定到窗口切换,所以我们不能使用它
tab-insert (M-TAB) Insert a tab character.
制表符插入 (M-TAB) 插入制表符。
--
——
Note: you can use this strategy with all sorts of unusual characters. Like a carriage return:
注意:您可以将此策略用于各种不寻常的字符。就像一个回车:
echo "^V^M" # CTRL+V, CTRL+M
This is because carriage return is ASCII 13, and Mis the 13th letter of the alphabet, so when you type ^M
, you get the 13th ASCII character. You can see it in action using ls^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, verbatimgets you get the literal character.
这是因为回车符是 ASCII 13,而M是字母表的第 13 个字母,所以当你输入 时^M
,你会得到第 13 个 ASCII 字符。您可以ls^M
在空提示下使用,看到它的运行情况,这将插入一个回车,使提示的行为就像您按回车一样。当这些字符被正常解释时,逐字逐字让你得到文字字符。
回答by Anonymous
Using echo to print values of variables is a common Bash pitfall. Reference link:
使用 echo 打印变量值是一个常见的 Bash 陷阱。参考链接:
回答by ATC
If you want to use echo "a\tb"
in a script, you run the script as:
如果要echo "a\tb"
在脚本中使用,请按以下方式运行脚本:
# sh -e myscript.sh
Alternatively, you can give to myscript.sh the execution permission, and then run the script.
或者,您可以向 myscript.sh 授予执行权限,然后运行该脚本。
# chmod +x myscript.sh
# ./myscript.sh
回答by yanghaogn
res="\t\tx"
echo -e "[${res}]"