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

Posted on 2011-10-10
Last Modified: 2013-12-26
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!
Question by:beer9
    LVL 31

    Accepted Solution

    I believe the second one is $(), which is equivalent of  back ticks ``

    You can use it for any evaluation like

    just like

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

    echo $((7*8))

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

    if (( x == 9 ))
    echo equal

    LVL 31

    Expert Comment

    $(( )) is for mathematical evaluation (finding a value)
    (( )) is for logical expression (true or false conditions)
    LVL 31

    Expert Comment

    (( )) is for mathematical logical conditions
    (( 7+8 == 15 )) && echo true

    [[ ]] is for non mathematical logic
    [[ $name == "beer9" ]] && echo equal
    LVL 19

    Assisted Solution

    (()) is also used to assign numerical values.

    For example, if $LL is 5, then after
    $LL will be 12.

    This is often used to increment a value:

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

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
    How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now