Linux批量命令中括号,linux脚本中单中括号和双中括号的区别

在 Bash 4.3.11 测试:

POSIX vs Bash 扩展:

[ 是 POSIX

[[ is 一个 Bash 扩展

常规命令 vs 魔法

[ 只是一个有着奇怪名字的常规命令。

] is just an argument of [ that prevents further arguments from being used.只是一个 [ 的避免更多的参数被使用的参数。

Ubuntu 16.04 actually has an executable for it at /usr/bin/[ provided by coreutils, but the bash built-in version takes precedence.

Nothing is altered in the way that Bash parses the command.

In particular, < is redirection, && and || concatenate multiple commands, ( ) generates subshells unless escaped by \, and word expansion happens as usual.

[[ X ]] is a single construct that makes X be parsed magically.

There are also further differences like = and =~.

In Bashese: [ is a built-in command, and [[ is a keyword: https://askubuntu.com/questions/445749/whats-the-difference-between-shell-builtin-and-shell-keyword

<

[[ a < b ]]: lexicographical comparison

[ a \< b ]: Same as above. \ required or else does redirection like for any other command. Bash extension.

I could not find a POSIX alternative to this, see: https://stackoverflow.com/questions/21294867/how-to-test-strings-for-less-than-or-equal

&& and ||

[[ a = a && b = b ]]: true, logical and

[ a = a && b = b ]: syntax error, && parsed as an AND command separator cmd1 && cmd2

[ a = a -a b = b ]: equivalent, but deprecated by POSIX

[ a = a ] && [ b = b ]: POSIX recommendation

(

[[ (a = a || a = b) && a = b ]]: false

[ ( a = a ) ]: syntax error, () is interpreted as a subshell

[ \( a = a -o a = b \) -a a = b ]: equivalent, but () is deprecated by POSIX

([ a = a ] || [ a = b ]) && [ a = b ] POSIX recommendation

word splitting

x='a b'; [[ $x = 'a b' ]]: true, quotes not needed

x='a b'; [ $x = 'a b' ]: syntax error, expands to [ a b = 'a b' ]

x='a b'; [ "$x" = 'a b' ]: equivalent

=

[[ ab = a? ]]: true, because it does pattern matching (* ? [ are magic). Does not glob expand to files in current directory.

[ ab = a? ]: a? glob expands. So may be true or false depending on the files in the current directory.

[ ab = a\? ]: false, not glob expansion

= and == are the same in both [ and [[, but == is a Bash extension.

printf 'ab' | grep -Eq 'a.': POSIX ERE equivalent

[[ ab =~ 'ab?' ]]: false, loses magic with ''

[[ ab? =~ 'ab?' ]]: true

=~

[[ ab =~ ab? ]]: true, POSIX extended regular expression match, ? does not glob expand

[ a =~ a ]: syntax error

printf 'ab' | grep -Eq 'ab?': POSIX equivalent

Recommendation

I prefer to always use [].

There are POSIX equivalents for every [[ ]] construct I've seen.

If you use [[ ]] you:

lose portability

force the reader to learn the intricacies of another bash extension. [ is just a regular command with a weird name, no special semantics are involved.

没翻译完,待续

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值