Results 1 to 9 of 9

Thread: BASH: math with variables

  1. #1
    Join Date
    May 2007
    Location
    Canada!
    Beans
    1,709
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    BASH: math with variables

    I would like to do some simple math for some code.

    ex.
    variableA=variableB*variableC

    what could be the code for this?

  2. #2
    Join Date
    Jan 2006
    Location
    Leuven, Belgium
    Beans
    3,414

    Re: BASH: math with variables

    variableA=$((variableB*variableC))

  3. #3
    Join Date
    May 2007
    Location
    Canada!
    Beans
    1,709
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: BASH: math with variables

    and what about

    variableA=variableA "Divided by" 1000 * 1024

  4. #4
    Join Date
    Jan 2006
    Location
    Leuven, Belgium
    Beans
    3,414

    Re: BASH: math with variables

    variableA=$(($variableA/1000*1024))

    Mind to use braces when needed, I wasn't sure whether you meant /1000*1024 or /(1000*1024).

  5. #5
    Join Date
    May 2007
    Location
    Canada!
    Beans
    1,709
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: BASH: math with variables

    thanks

    I still needed to do a little testing, but I guess it's better to do the multiplying first and then divide

  6. #6
    Join Date
    Oct 2004
    Location
    Pennsylvania
    Beans
    1,698

    Re: BASH: math with variables

    Quote Originally Posted by ryanVickers View Post
    thanks

    I still needed to do a little testing, but I guess it's better to do the multiplying first and then divide
    Note that bash does integer math. If you need fractions, you need another program like "bc" or "awk".

  7. #7
    Join Date
    May 2007
    Location
    Canada!
    Beans
    1,709
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: BASH: math with variables

    ok then. This is perfect for now though

  8. #8
    Join Date
    Feb 2007
    Beans
    Hidden!

    Re: BASH: math with variables

    Wow, 4 years later, just the answer I was looking for. Thanks!

  9. #9
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

Bookmarks

Posting Permissions

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