为什么一个变量赋值用空格替换制表符 [英] Why a variable assignment replaces tabs with spaces

查看:136
本文介绍了为什么一个变量赋值用空格替换制表符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么一个变量赋值,在外壳的空间替换标签?

  $猫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 under 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

...

这篇关于为什么一个变量赋值用空格替换制表符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆