[[ is not a "better version" of [

[ is a just a command like everything else in your bash script. you should expect everything what you expect from other commands and not more.

[[ and ]] are reserved words in between which your expections about shell's behavior, which hold in every other area in the script, and learned oddities which you may found unintuituve when you started to learn shell but later get used to, should be suspended.

eg. there is no pathname expansion (*, ?) in [[ ]]], except for leading tilde (~).

= does not mean equal, it means fnmatch(). as does == as well.

no word-splitting, except if you actually don't want: [[ $x =~ a[ ]+b ]]

most people "converted" to [[ just because they want shell special chars, especially < and >, to be not-special (only  when they put them after an "if"). so they get nice readable arithemtic comparision. so you get it nice. not reasonable but nice:

[[ +0 > -0 ]]  # true

[[ 02 < 1 ]]  # true

[[ -1 < 1 ]]  # true unless you installed NLS collation dictionaries because then minus 1 is greater than plus 1

don't argue that it's reasonable for everyone read the docs, because then [ "$a" "<" "$b" ] is not less reasonable either.

Hozzászólások

Anyám... Még ezt sem tudtam..

Köszi!

mi értesz anti pattern alatt?

úgy tudom azt inkább arra használjuk hogy pl esetünkben [-t vagy [[-t használjon  az ember, ugye az pattern mert ismételten használja ezt vagy azt.

a post meg példákat sorolt fel  [[  pitfall-okra.

akkor most az "ezek" alatt vagy a [ és [[-re gondolsz, amik helyett nem tudom mi lenne az ami nem anti pattern feltételvizsgálatra bash-ben; vagy a [[ működésének kontraintuitív példáira, azokban meg ami "erős" az épp az hogy sok fejlesztő várakozásainak ellentmond – mi bashban elő szokott fordulni ugyan, csakhogy a [[ egy új fejezetét nyitja meg a nemvárt működéseknek, míg a [ meg abban mozog mint a nyelv minden más gyakran használt területe – tehát épp alátámasztja Honkydoo észrevételét, nem ellentétes vele.