Shell关于空格那点事儿

来源:互联网 发布:linux tcp ip协议栈 编辑:程序博客网 时间:2024/04/29 06:41

最近公司要用到shell 所以开始研究shell 研究了一天 找到了一些问题

空格,一个看不见的字符,很不起眼,也正由于不起眼,很多人经常忽略它,导致代码出错,却还找不着北。这里,我们来聊聊空格那点事儿

首先 赋值时‘=’两边不能有空格

[root@localhost baipengfei]# name = baipengfeibash: name: command not found[root@localhost baipengfei]# name= baipengfeibash: baipengfei: command not found[root@localhost baipengfei]# name =baipengfeibash: name: command not found[root@localhost baipengfei]# name=baipengfei[root@localhost baipengfei]# echo $namebaipengfei[root@localhost baipengfei]# 
做了一些尝试的确绝对不要有空格否则就等着报错吧。


再者命令和选项之间必须有空格,今天写一个shell脚本

if["$xx"-ne"$xx"]then   echo xxxxxelse    echo xxxxxxfi


这样编写时错误的缺少空格

命令之间一定要有空格

正确的

if [ "$xx" -ne "$xx" ]then   echo xxxxxelse    echo xxxxxxfi

不论少了那一个空格都会报错

最后管道命令两边的空格是可有可无的无所谓我就不去验证了