最近公司要用到shell 所以开始研究shell 研究了一天 找到了一些问题
空格,一个看不见的字符,很不起眼,也正由于不起眼,很多人经常忽略它,导致代码出错,却还找不着北。这里,我们来聊聊空格那点事儿
首先 赋值时‘=’两边不能有空格
[root@localhost baipengfei]# name = baipengfei
bash: name: command not found
[root@localhost baipengfei]# name= baipengfei
bash: baipengfei: command not found
[root@localhost baipengfei]# name =baipengfei
bash: name: command not found
[root@localhost baipengfei]# name=baipengfei
[root@localhost baipengfei]# echo $name
baipengfei
[root@localhost baipengfei]#
做了一些尝试的确绝对不要有空格否则就等着报错吧。
再者命令和选项之间必须有空格,今天写一个shell脚本
if["$xx"-ne"$xx"]
then
echo xxxxx
else
echo xxxxxx
fi
这样编写时错误的缺少空格
命令之间一定要有空格
正确的
if [ "$xx" -ne "$xx" ]
then
echo xxxxx
else
echo xxxxxx
fi
不论少了那一个空格都会报错
最后管道命令两边的空格是可有可无的无所谓我就不去验证了