[]与[[]]的区别
bash shell中[[ ]]与[ ]的区别
一,[[用"&&"而不是"-a"表示逻辑"与",用"||"而不是"-o"表示逻辑"或":
$ [[ 1 a ]]&&echo true||echo false
true
$ [[ 1 a ]]&&echo true||echo false
bash: syntax error in conditional expression
bash: syntax error near `-a’
$ [ 1 a ]&&echo true||echo fal...
bash shell中[[ ]]与[ ]的区别
一,[[用"&&"而不是"-a"表示逻辑"与",用"||"而不是"-o"表示逻辑"或":
$ [[ 1 < 2 && b > a ]]&&echo true||echo false
true
$ [[ 1 < 2 -a b > a ]]&&echo true||echo false
bash: syntax error in conditional expression
bash: syntax error near `-a’
$ [ 1 < 2 -a b > a ]&&echo true||echo false
true
$ [ 1 < 2 && b > a ]&&echo true||echo false #wrong syntax
bash: [: missing `]‘
false
二,[ ... ]为 shell命令,所以在其中的表达式应是它的命令行参数,所以串比较操作符”>” 与”<”必须转义,否则就变成 IO改向操作符了。[[中"<"与">"
不需转义:
$ [ 2 \< 10 ]&&echo true||echo false #you should use “\<”
false
$ [[ 2 < 10 ]]&&echo true||echo false
false
三,[[ ... ]]进行算术扩展,而[ ... ]不做。
$ [[ 99+1 -eq 100 ]]&&echo true||echo false
true
$ [ 99+1 -eq 100 ]&&echo true||echo false
bash: [: 99+1: integer expression expected
false
$ [ $((99+1)) -eq 100 ]&&echo true||echo false
true
四,[[]]能用正则,而[]不行
[root@test ccc]# [ "test.php" == *.php ] && echo true || echo false
false
[root@test ccc]# [[ "test.php" == *.php ]] && echo true || echo false
true
[root@test ccc]# [[ "t.php" == [a-z].php ]] && echo true || echo false
true
[root@test ccc]# [ "test.php" == "*.php" ] && echo true || echo false
false
[root@test ccc]# [[ "test.php" == "*.php" ]] && echo true || echo false
false
bash shell中[[ ]]与[ ]的区别
本文档为【[]与[[]]的区别】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。