Please use ***
[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do
not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.
Another aspect of "
[" being a command is that it's very strict about the number and type of arguments that follow it. A two-value comparison must have exactly four arguments following it,
valueA,
comparator,
valueB,
end-bracket. If you're using a variable or something that expands into more values than that, or
less, you're going to have trouble:
Bash Pitfall #4, among others.
So remember to always quote your arguments.
That's why, as millgates said, when using advanced shells like
bash or
ksh it's recommended to use
[[..]] for string/file tests, and
((..)) for numerical tests. Avoid using the old
[..] test unless you specifically need POSIX-style portability.
Being built-in keywords, rather than separate commands, they can parse their arguments more intelligently, and so have fewer limitations than the old test.
http://mywiki.wooledge.org/BashFAQ/031
http://mywiki.wooledge.org/ArithmeticExpression
Now for one more thing. As the
BashGuide says, "
expr is a relic of ancient Rome. Do not wield it.".
There is nothing that
expr does that cannot be more efficiently done with modern shell built-ins. Unless you're using a truly ancient shell, put it away and forget about it.
In fact, AFAICT, there's only thing
expr does that
bash can't do
directly, and that's return the index number of a character in a string. But even that can be worked around very easily with a couple of simple
parameter substitutions.
Code:
$ string=foobarbazbumfoobarbazbum
$ expr index "$string" b
4
$ n="${string%%b*}_" ; echo "${#n}"
4
In fact, it's even more flexible, because you can also get the index of the last occurrence the same way, as well as other things like the beginning of longer strings:
Code:
$ n="${string%b*}_" ; echo "${#n}"
22
$ n="${string%%baz*}_" ; echo "${#n}"
7
$ n="${string%baz*}_" ; echo "${#n}"
19