shell脚本没问题的问题

use*_*207 3 bash ubuntu-12.04

我有两个相同的Ubuntu 12.04 LTS系统.今天,一个shell脚本在其中一个Ubuntu系统上失败,即使它已经多年未被修改,并且几个小时前就已经开始工作了.我能够将错误缩小到这个:

string='question mark: ?'
echo $string
question mark: n t

string='question mark: ?'
echo "$string"
question mark: ?
Run Code Online (Sandbox Code Playgroud)

在shell脚本失败的系统上,'?' 当省略引号时,将替换为'nt'.这是对其他相同系统进行相同测试的结果:

string='question mark: ?'
echo $string
question mark: ?

string='question mark: ?'
echo "$string"
question mark: ?
Run Code Online (Sandbox Code Playgroud)

在此系统上,无论是否省略引号,都会正确打印问号.

shell脚本多年未被修改,系统尚未以任何方式升级或修改.shell脚本在几个小时前工作正常,但由于'?'突然失败了 由于没有明显的原因,它被转换为'n t'.

我因此而疯狂,所以我真的希望有人知道为什么会这样.

谢谢.

fed*_*qui 8

这是因为您有文件nt工作目录.?被任何带有一个字符的文件替换.

$ ls
a  t myfile

$ echo $string
question mark: a t
Run Code Online (Sandbox Code Playgroud)

要解决此问题,请在以下情况下双引号变量echo:

$ echo "$string"
question mark: ?
Run Code Online (Sandbox Code Playgroud)

一般来说,为什么bash"echo [t]"导致"t"而不是"[t]"中的devnull注释:

Shell命令语言 告诉shell,以下字符是特殊的,具体取决于上下文:

*   ?   [   #   ~   =   %
Run Code Online (Sandbox Code Playgroud)

此外,如果要表示自己,必须引用以下字符:

|  &  ;  <  >  (  )  $  `  \  "  '  <space>  <tab>  <newline>
Run Code Online (Sandbox Code Playgroud)

例如,*你得到所有文件:

$ string='question mark: *'

$ echo $string
question mark: a myfile t
Run Code Online (Sandbox Code Playgroud)