bash shell脚本中什么时候该用空格?哪些情况下不用空格?
常见的问题
i@itnetcn ~ $ var1 = test
bash: var1: command not found i@itnetcn ~ $ echo ${var1:?error} bash: var1: error i@itnetcn ~ $ echo ${var1?error} bash: var1: error i@itnetcn ~ $ var2 =test bash: var2: command not found i@itnetcn ~ $ echo ${var2:?error} bash: var2: error i@itnetcn ~ $ echo ${var2?error} bash: var2: error
2. 赋值时等号左边没有空格,右边有空格(两种情况)
i@itnetcn ~ $ var= test
i@itnetcn ~ $ var= nocmd bash: nocmd: command not found
同样是等号右边有空格,第一条命令没报错,而第二条报错了。
例子:
i@itnetcn ~ $ var=newtest eval echo \$var
newtest i@itnetcn ~ $ echo $var 注意,这里使用了eval, 是想避免在第一次解析时$var被替换成空字符串,不然就会出现以下情况(下面是错误的测试方法,在echo还没执行时,$var已经被替换成空字符串)字符串替换
例子:
i@itnetcn ~ $ var=newtest echo $var
i@itnetcn ~ $ echo $var
3. 命令和选项之间必须有空格
例子:
i@itnetcn ~ $ if [ "abc" = "abc" ]; then echo ‘they are the same'; fi
they are the same i@itnetcn ~ $ type -a [ [ is a shell builtin [ is /usr/bin/[
[命令经常用到if判断中,有人喜欢这么写:
复制代码代码示例:
i@itnetcn ~ $ [ "abc" = "cba" ] || echo ‘they are not the same'
they are not the same i@itnetcn ~ $ type -a [ [ is a shell builtin [ is /usr/bin/[
[ 命令正名叫test命令,它们两者几乎一样,为什么不是完全一样?来看看这个
i@itnetcn ~ $ [ "abc" = "cba"
bash: [: missing `]‘ i@itnetcn ~ $ [ "abc" = "cba" ] i@itnetcn ~ $ test "abc" = "cba" ] bash: test: too many arguments
i@itnetcn ~ $ test "abc" = "cba" 关于[常见的错误
a. if 与 [ 之间缺少空格
i@itnetcn ~ $ if[ "$HOME" = "/home/i"];then echo 'equal'; fi
bash: syntax error near unexpected token `then' i@itnetcn ~ $ if[ "$HOME" = "/home/i" ];then echo 'equal'; fi bash: syntax error near unexpected token `then' i@itnetcn ~ $ if["$HOME" = "/home/i"];then echo 'equal'; fi bash: syntax error near unexpected token `then' i@itnetcn ~ $ if["$HOME" = "/home/i" ];then echo 'equal'; fi bash: syntax error near unexpected token `then' 语法分析错误,很明显,if[ 对于bash来说,不知道是什么鬼东西
b. [与后面的参数之间缺少空格
i@itnetcn ~ $ if ["$HOME" = "/home/i" ];then echo 'equal'; fi
bash: [/home/i: No such file or directory i@itnetcn ~ $ if ["$HOME" = "/home/i"];then echo 'equal'; fi bash: [/home/i: No such file or directory ["$HOME" 对于bash来说,也不知道是什么鬼东西
c. [ ] 之间的参数之间缺少空格
复制代码代码示例:
i@itnetcn ~ $ if [ "abc"="abc" ]; then echo 'equal'; fi
equal i@itnetcn ~ $ if [ "abc"="cba" ]; then echo 'equal'; fi equal
第一条命令似乎是对的(实际上是正巧而已),看看第二条命令"abc" 和 "cba"明显不同,但却判断为相同。 例子:
i@itnetcn ~ $ if [ 0 ]; then echo 'equal'; fi
equal i@itnetcn ~ $ if [ "1" ]; then echo 'equal'; fi equal i@itnetcn ~ $ if [ "" ]; then echo 'equal'; fi i@itnetcn ~ $ if [ ]; then echo 'equal'; fi 在[ ] 内部,如果只有一个值(那些因为缺少了空格而连一起的也算),不是空字符串就为真。所以在[ ] 之间的参数,也要两边有空格,而不能堆一起
d. 参数和尾巴]之间缺少空格
i@itnetcn ~ $ var=' abc'
i@itnetcn ~ $ if [$var = "abc" ];then echo 'equal'; fi equal i@itnetcn ~ $ if ["$var" = "abc" ];then echo 'equal'; fi bash: [ abc: command not found 之前Bash引号那点事提到过,双引号包围起来的是一个整体,而没双引号的时候,字符串前后的空格或制表符都被切开。
如果恰巧遇到了或故意要丢弃字符串前后的空格或制表符,那也不是不可能,但极其不建议这样写。
例子:
i@itnetcn ~ $ var=''
i@itnetcn ~ $ if [ "$var" = "abc" ];then echo 'equal'; fi i@itnetcn ~ $ if [ $var = "abc" ];then echo 'equal'; fi bash: [: =: unary operator expected i@itnetcn ~ $ dvar='a b c' i@itnetcn ~ $ if [ $dvar = "a b c" ];then echo 'equal'; fi bash: [: too many arguments i@itnetcn ~ $ if [ "$dvar" = "a b c" ];then echo 'equal'; fi equal 再次提醒:不要轻易省略双引号。 (责任编辑:IT) |