在 bash 脚本中回显制表符

发布于 2024-07-13 12:24:15 字数 211 浏览 7 评论 0原文

如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

阿楠 2024-07-20 12:24:15
echo -e ' \t '

将回显“空格制表符空格换行符”(-e 表示“启用反斜杠转义的解释”):

$ echo -e ' \t ' | hexdump -C
00000000  20 09 20 0a                                       | . .|
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                                       | . .|
月亮坠入山谷 2024-07-20 12:24:15

使用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 将输出存储在变量中)。

对于您的示例:

res='           'x # res = "\t\tx"
printf '%s\n' "[$res]"

现在是时候让我承认 echo 对于您所询问的示例同样有效; 你只需要在参数周围加上双引号:

echo "[$res]"

正如 kmkaplan 所写(两年半前,我刚刚注意到!)。 原始命令的问题:

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

不在于 echo; 这是因为 shell 在 echo 看到它之前就用空格替换了制表符。

echo 适合简单输出,例如 echo hello world,但只要您想做更复杂的事情,就应该使用 printf。 您可以echo工作,但是当您使用不同的echo实现或不同的shell运行它时,生成的代码可能会失败。

Use printf, not 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.

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).

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 "[$res]"

as kmkaplan wrote (two and a half years ago, I just noticed!). The problem with your original commands:

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 is fine for simple output, like echo hello world, but you should use printf whenever you want to do something more complex. You can get echo to work, but the resulting code is likely to fail when you run it with a different echo implementation or a different shell.

剪不断理还乱 2024-07-20 12:24:15

您还可以尝试:

echo Hello
\t'world.

You can also try:

echo Hello
\t'world.
小情绪 2024-07-20 12:24:15

将字符串放在引号之间:

echo "[$res]"

Put your string between double quotes:

echo "[$res]"
阿楠 2024-07-20 12:24:15

你需要使用 -e 标志来回显然后你可以

echo -e "\t\t x"

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

echo -e "\t\t x"
天涯沦落人 2024-07-20 12:24:15

从 bash 手册页:

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

所以你可以这样做:

echo 
hello\tworld'

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'
饭团 2024-07-20 12:24:15

使用逐字击键 ^VCTRL+VCv随便)。

当您在终端(或大多数 Unix 编辑器)中键入 ^V 时,将逐字记录以下字符。 您可以使用它在您正在回显的字符串中键入文字制表符。

类似以下内容的作品:

echo "^V<tab>"     # CTRL+V, TAB

Bash 文档 (qv ,“引用插入”)

引用插入(Cq、Cv)
将您键入的下一个字符逐字添加到该行。 例如,这就是如何插入像 Cq 这样的按键序列。

旁注:根据此, ALT+TAB 应该做同样的事情,但我们都将该序列绑定到窗口切换,因此我们不能使用它

制表符插入 (M-TAB)
插入制表符。

--

注意:您可以对各种不寻常的角色使用此策略。 像回车符:

echo "^V^M"        # CTRL+V, CTRL+M

这是因为回车符是 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:

echo "^V<tab>"     # CTRL+V, TAB

Bash docs (q.v., "quoted-insert")

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.

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

tab-insert (M-TAB)
Insert a tab character.

--

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 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 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, verbatim gets you get the literal character.

婴鹅 2024-07-20 12:24:15

使用 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

夜夜流光相皎洁 2024-07-20 12:24:15

如果您想在脚本中使用 echo "a\tb",请按以下方式运行脚本:

# sh -e myscript.sh

或者,您可以向 myscript.sh 授予执行权限,然后运行该脚本。

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

If you want to use echo "a\tb" in a script, you run the script as:

# sh -e myscript.sh

Alternatively, you can give to myscript.sh the execution permission, and then run the script.

# chmod +x myscript.sh
# ./myscript.sh
肩上的翅膀 2024-07-20 12:24:15
res="\t\tx"
echo -e "[${res}]"
res="\t\tx"
echo -e "[${res}]"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文