为什么一个变量赋值用空格替换制表符 [英] Why a variable assignment replaces tabs with spaces
问题描述
为什么一个变量赋值,在外壳的空间替换标签?
$猫TMP
一个BéC D
$水库= $(猫TMP)
$回声$水库
一个BéC D
您需要引用您的变量 $水库
的空白是preserved。
$猫文件
一个BéC D$水库= $(CAT文件)$回声$水库
一个BéC D$回声$水库
一个BéC D
从男人庆典
在 QUOTING
:
引用用来去除某些字符的特殊含义
或单词到shell。
引用可以用来禁止特殊处理的特殊字符,以prevent
从保留字被识别,还和prevent参数扩展。
每个下定义上面列出的元字符有特殊含义壳
而且必须用引号引起来,如果它是重新present本身。...\\ A警报(钟)
\\ b退格
\\ E
\\ E转义字符
\\ f换
\\ n新行
\\ r回车
\\ t水平制表
\\ v垂直制表
\\\\反斜杠
\\'单引号
\\双引号
\\ NNN 8位字符,其值是八进制值NNN
\\ XHH 8位字符,它的值是十六进制值HH
\\ CX控制-X字符...
块引用>Why does a variable assignment replace tabs with spaces in the shell?
$ cat tmp a b e c d $ res=$(cat tmp) $ echo $res a b e c d
解决方案You need to quote your variable
$res
for whitespace to be preserved.$ cat file a b e c d $ res=$(cat file) $ echo $res a b e c d $ echo "$res" a b e c d
From
man bash
underQUOTING
: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 ...
这篇关于为什么一个变量赋值用空格替换制表符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!