天天看点

$ 符号在脚本中的使用

(1)《The Linux Command Line》  P94  Arithmatic Expansion

  Arithmatic Expansion

$ 符号在脚本中的使用

(2)《The Linux Command Line》  P99  Double Quotes

双引号中,除“$ \ ' ”外,其他的特殊字符都会失去原有的特殊意义:

$ 符号在脚本中的使用

双引号特殊使用情况:

  case 1: 某个文件名为:two word.txt (中间有空格),使用命令:ls -l two word.txt ,会出错。使用ls -l “two word.txt”,就是正确的。

      但是现在的系统不用这样做了,使用自动补全,系统会自动识别空格

  case 2:

$ 符号在脚本中的使用

  case 3:

$ 符号在脚本中的使用

  case 4:

$ 符号在脚本中的使用

(3)  《The Linux Command Line》  P102  Escaping Characters  在Backslash escape sequences中,用 $' ' 来使用escape sequence

  case 1:想显示某人的余额  echo "The balance for the user of $USER is: $5.00"  

      结果会出问题:不会显示 The balance for the user of root is: $5.00 ,

      而是 The balance for the user of root is: .00

  case 2:有一个文件的名字是 bad&filename ,想要使用命令行将其重命名,  

      mv bad&filename good_filename

      结果会是错误的。

以上两种情况都想使用具有特殊意义的符号:$ & ,在双引号中和文件名中使用,要在其前加上 \ ,echo "The balance for the user of $USER is: \$5.00"

  mv bad\&filename good_filename

这样就引出了 Backslash escape sequences。其中:\a \b \t \n 是常用的,要在 echo 命令中使用它们,就必须使用 -e 选项,来解释这些backslash

  echo -e "Hello!\n"

也可以使用另一种:echo "Hello" $'\n'

(4)《The Linux Command Line》  P107  Completion  输入命令时,命令中某一个 word 以 $ 开头,那么这个 word 是一个变量

$ 符号在脚本中的使用

转载于:https://www.cnblogs.com/bkyysd/p/4293880.html

继续阅读