Find the answer to your Linux question:
Results 1 to 4 of 4
Like Tree2Likes
  • 1 Post By Irithori
  • 1 Post By watael
The following line of code should result in GROUPS holding a number from 1 to probably 4 or 5. I need the next line to simply add 1 to whatever ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    Dec 2010
    Posts
    122

    adding one to a variable in bash


    The following line of code should result in GROUPS holding a number from 1 to probably 4 or 5. I need the next line to simply add 1 to whatever this lines results to. I've tried a lot of tings in bash but I can't make it happen. Any ideas? Thanks. GROUPS=$( expr ${NUMBEROFDIGITS} / 3 )

    I thought it would be as simple as ${GROUPS}=${GROUPS+1} or }+1 but nothing I've tried has worked.

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,422
    Carefull, $GROUPS is a predefined variable of bash.
    Choose a different name to avoid unwanted sideeffects.

    As for bash arithmetics, this example should help
    Code:
    #!/usr/bin/env bash
    
    a=1
    let a=a+1
    echo $a
    Garrett85 likes this.
    You must always face the curtain with a bow.

  3. #3
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    229
    hi,

    the POSIX way:
    Code:
    var=1
    var=$(( $var + 1 ))
    echo $var
    2
    the BASH way
    Code:
    var=1
    echo $((++var))
    2
    Garrett85 likes this.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Newbie
    Join Date
    Dec 2010
    Posts
    122
    Thanks. I was trying the let command as well but I was wrapping the variable in ${}. I didn't realize you didn't need the $ with let.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •