gpt4 book ai didi

bash - 为什么变量赋值用空格替换制表符

转载 作者:行者123 更新时间:2023-11-29 08:49:08 25 4
gpt4 key购买 nike

为什么变量赋值会在 shell 中用空格替换制表符?

$ cat tmp
a b e c d
$ res=$(cat tmp)
$ echo $res
a b e c d

最佳答案

您需要引用变量 $res 以保留空白。

$ cat file
a b e c d

$ res=$(cat file)

$ echo $res
a b e c d

$ echo "$res"
a b e c d

来自 man bash 下的 QUOTING:

Quoting is used to remove the special meaning of certain characters or words to the shell. Quoting can be used to disable special treatment for special characters, to prevent reserved words from being recognized as such, and to prevent parameter expansion.

Each of the metacharacters listed above under DEFINITIONS has special meaning to the shell and must be quoted if it is to represent itself.

...

\a alert (bell)
\b backspace
\e
\E an escape character
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\\ backslash
\' single quote
\" double quote
\nnn the eight-bit character whose value is the octal value nnn
\xHH the eight-bit character whose value is the hexadecimal value HH
\cx a control-x character

...

关于bash - 为什么变量赋值用空格替换制表符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14231232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com