shell - 用引号括起变量

时间:2018-03-07 05:13:34

标签: shell unix

有人可以解释我在shell脚本中用引号包装变量的用法是什么。 我知道绑定两个或更多字符串会很有帮助,但如果我有一个字符串/整数,目的是什么。

X=0
while [ -n "$X" ]
do
   echo "Enter some text (RETURN to quit)"
   read X
   if [ -n "$X" ]; then
      echo "You said: $X"
   fi
done

如果我没有在第2行的引号中保留$ X,即使按下RETURN,脚本也不会终止。

1 个答案:

答案 0 :(得分:1)

在调试模式下运行脚本,以查看有关如何解释条件的区别。

+ '[' -n '' ']'
  1. 当提到引号时 - + '[' -n ']'
  2. 未提及引号时true(始终为X
  3. 按下RETURN键时,

    var sb = new StringBuilder(); for(int index = 1; index <= 20; index++) { var tb = (TextBox)this.Controls.Find($"X{index}TB", true).FirstOrDefault; sb.AppendLine(tb.Text); } // Save the contents of the file. var write = new StreamWriter(File.Create(save.FileName)); write.Write(sb.ToString()); 将为空,但解释器仅在引用时才会考虑。