shell下变量比较最佳实践
https://stackoverflow.com/questions/13617843/unary-operator-expected
If you know you're always going to use bash, it's much easier to always use the double bracket conditional compound command [[ ... ]]
, instead of the Posix-compatiblesingle bracket version [ ... ]
. Inside a [[ ... ]]
compound, word-splitting and pathname expansion are not applied to words, so you can rely on
if [[ $aug1 == "and" ]];
to compare the value of
$aug1
with the string and
.If you use
[ ... ]
, you always need to remember to double quote variables like this:if [ "$aug1" = "and" ];
If you don't quote the variable expansion and the variable is undefined or empty, it vanishes from the scene of the crime, leaving only
if [ = "and" ];
which is not a valid syntax. (It would also fail with a different error message if
$aug1
included white space or shell metacharacters.)The modern
[[
operator has lots of other nice features, including regular expression matching.【shell下变量比较最佳实践】转载于:https://www.cnblogs.com/codeking100/p/10235623.html
推荐阅读
- 开学第一天(下)
- Shell-Bash变量与运算符
- 【故障公告】周五下午的一次突发故障
- 生活随笔|好天气下的意外之喜
- MongoDB,Wondows下免安装版|MongoDB,Wondows下免安装版 (简化版操作)
- 汇讲-勇于突破
- Android中的AES加密-下
- 说的真好
- (二)ES6第一节变量(let|(二)ES6第一节变量(let,const)
- 放下心中的偶像包袱吧