Vern
Vern

Reputation: 2413

Bash: '$(( ))' means 'expr' and '[ ]' means 'test'?

I have been working with some bash scripting lately and been looking through the man pages. From what I have gathered, does $(( )) mean expr and [ ] mean test?

For $(( )):

echo $(( 5 + 3 ))

has the same output as:

echo $(expr 5 + 3)

For [ ]:

test 'str' = 'str'

has the same success value as:

[ 'str' = 'str' ]

Did I get my understanding right?

Upvotes: 5

Views: 727

Answers (2)

Lesmana
Lesmana

Reputation: 27073

the ((...)) construct is equivalent to the bash builtin let. let does mostly the same stuff which expr does.

the $((...)) construct, note the $ at the beginning, will substitute the output of the expression inside just like $(...) does.

the [...] construct is in fact just another name for test.

see the bash help pages for more information.

  • help "("
  • help let
  • help [
  • help test

see also:

Upvotes: 9

mouviciel
mouviciel

Reputation: 67859

You are correct about [ ] and test

About $(( )), this is a more elaborate replacement of expr. You can compute more complex expressions than with expr.

Upvotes: 6

Related Questions