The first and last versions both look and act correctly for me. Are you sure you are using bash
and not sh
?
Note that [
and [[
are both "commands" (built-ins really, I think is the appropriate term) and are slightly different. Basically you want to use [[
unless you want legacy compatibility with old shells. If using [
there are additional considerations, for example it can cause problems with empty string comparisons.
Also as an aside, the 2nd version should generally work, but you get the problems mentioned above - and the 3rd version is not good.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…