为什么变量赋值会在 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/