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

标签 bash shell variables

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

相关文章:

linux - 在 bash 脚本中分组时,Shell 命令不起作用

c - 在 C 中实现管道时出现错误的文件描述符错误

java - 在抽象类中初始化最终变量 (Java)

linux - 重新排序文本文件中的行以获得更好的压缩率

linux - bc 不通过​​脚本执行

Linux Shell,如何通过串行端口发送命令并返回?

java - 编程变量问题(java)

linux - bash 脚本中未显示参数

linux - 发送电子邮件和通知发送在 crontab 中不起作用

Java在方法中将全局变量转换为局部变量的优点