[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

Difference of (( )) and $(( )) in bash script

Hello, While going through reading some bash tutorial I am stuck here..

(( )) Evaluates and assigns value to a variable and does math in a shell
$(( )) Evaluates the enclosed expression

Open in new window


I am not able to get where should I use (( )) and $(( )). Could someone please provide me an example to understand it better. Thanks!
0
beer9
Asked:
beer9
  • 3
2 Solutions
 
farzanjCommented:
I believe the second one is $(), which is equivalent of  back ticks ``

You can use it for any evaluation like

DATE=$(date)
FILES=$(ls)
just like
DATE=`date`

To evaluate any mathematical expression, you can use this (some math of shell)
echo $((5+6))

echo $((7*8))


Third:
When you want to evaluate a mathematical expression for condition, use (( ))
(( 5+6 == 11 )) && echo true

x=9
if (( x == 9 ))
then
echo equal
fi

0
 
farzanjCommented:
$(( )) is for mathematical evaluation (finding a value)
(( )) is for logical expression (true or false conditions)
0
 
farzanjCommented:
(( )) is for mathematical logical conditions
(( 7+8 == 15 )) && echo true

[[ ]] is for non mathematical logic
name=beer9
[[ $name == "beer9" ]] && echo equal
0
 
simon3270Commented:
(()) is also used to assign numerical values.

For example, if $LL is 5, then after
    ((LL=LL+7))
$LL will be 12.

This is often used to increment a value:
  ((LL++))

Note the lack of $ signs - compare with the more classical
  LL=$(expr $LL + 1)
 
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now