11

I'm new to bash programming. I've read two good, long guides, but they made a mess in my head about operators and keywords.

Which are more commonly and widely used? I don't know when to use which and why.

  1. test, [[ ]] or [ ] or even (( ))
  2. $((exp)) or $[exp]
  3. -gt or >, ge or >= etc.
  4. when to use < and > or ( ) and when we have to escape it with \: \< \( etc.
  5. == or = (string comparisons)

I know the basic differences between [ and [[ but other questions are a mystery for me.

Thank you in advance

1
  • 2
    A very useful wiki that is up to date and mentions much good practice is Greg's wiki. The Bash guide and the FAQ on there are particularly useful.
    – user76204
    Mar 27, 2013 at 18:32

1 Answer 1

5
  • test is a shell built-in which (obviously) tests for some given condition. Some older shells need a special command. So you'll find also a program at /usr/bin/test.
  • [ is also a program to test for some condition. This software needs also a closing bracket and you can find it at /usr/bin/[.
  • [[…]] is an alternative to test and [. It was developed for the Korn shell (ksh). But you also find it in Bash versions greater than 2 and in the Z Shell. The double brackets have some nifty features:
    • The shell does no word splitting or file name expansion.
    • You need no quoting.
    • Instead of -a (AND) or -o (OR) you can use && or ||.
    • The = can do a lot more.
  • ((…)) is equivalent to let. So basically ((expression)) is the same as let "expression". However with let you can use more than one expression, but double braces only allow one expression.
  • $((…)) (and also $[…]) does some calculation. You shell tries to calculate the expression inside the braces and replaces the expression with the result. So echo $((1+1)) leads to echo 2. So your shells prints the number 2.

Every time when you want to compare two numbers you should use the option with a dash and two letters (-ge, -lt etc.). If you want to compare strings you should use = or !=.

Your question regarding escaping is quite hard to answer. Because it depends on the software you use besides from the shell. So i.e. grep and grep -E need different escaping. This is also the case with sed, awk and others. So the best option is to have a look at the manpage. After some time you get accustomed and know when to use escaping.

3
  • thank you. one more question - because its possible to do comparison using ((...)) - is it more common to use this version: if (( 2 > 1 )) OR this: if [[ 2 -gt 1 ]]
    – MikeD
    Mar 29, 2013 at 23:34
  • I would assume that if [ 2 -gt 1 ]; … is more common, because I see this more often in shell scripts and as far as I know this is POSIX compliant.
    – qbi
    Mar 30, 2013 at 7:14
  • Another thing to keep in mind is that [ expression ] is true if expression evaluates to/returns zero, but (( expression )) is true if expression evaluates to/returns non-zero.
    – Joe
    Apr 4, 2013 at 6:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .