52

Published on December 2016 | Categories: Documents | Downloads: 61 | Comments: 0 | Views: 474
of 1
Download PDF   Embed   Report

Comments

Content

Linux Hints, Tips, and Tricks

September 10, 2007
Calculations in Scripts
To do simple calculations in bash, the ((…)) command can be used:
$ echo $(( 10 + 10 )) 20

To do floating point or more complex math, try the bc command:
$ echo "scale=5; 5/3" | bc 1.66666

To convert hex values to decimal, the ((…)) or bc command can be used:
$ echo $((0xff)) 255 $ echo 'obase=10; ibase=16; FF' | bc 255

Note that the hex value must be in uppercase letters if you use the bc command. To convert decimal to hex:
$ echo 'obase=16; ibase=10; 255' | bc FF

To convert hex to binary:
$ echo 'obase=2; ibase=16; FF' | bc 11111111

To do unit conversions, you can use the units command:
$ units -t '1mile' 'km' 1.609344

Or to do definition lookups:
$ units -t '1 googol' Definition: 1e+100

Filed under: Linux, Scripting — werner @ 6:52 pm Comments (0)

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close