3

What is the main difference between $(()) and expr in Unix-Like systems?

gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
SuckLips
  • 55
  • 1
  • 11
  • 1
    @andlrc That isn't relevant at all. – 123 Jun 08 '16 at 10:48
  • 1
    `$(())` is for bash arithmetic. expr is for evaluating expressions, this can be arithmetic or strings. Looking in the bash man page for `$(())` and the `expr` man page will tell you everything both commands can do. – 123 Jun 08 '16 at 10:50
  • Not just bash arithmetic. `$(( ))` has been in the POSIX sh spec since it was published in 1991. – Charles Duffy Jun 22 '16 at 14:44

2 Answers2

3

$(()) will almost certainly be within the shell that you are using. expr may be an external call

gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
Pwl256
  • 56
  • 1
  • Both are specified by POSIX. You might be able to construct a scenario where it is useful to use a command rather than built-in syntax for arithmetic, but by and large `$((...))` has replaced `expr` for all but regular expressions. – chepner Jun 08 '16 at 11:30
0

expr is a holdover from the early days of the Bourne shell. You should avoid expr unless you are forced to create scripts which need to be interoperable with legacy (pre-POSIX) sh implementations. (It's not too long ago since Solaris sh fell into this category, for example.)

tripleee
  • 175,061
  • 34
  • 275
  • 318