0% found this document useful (0 votes)
67 views1 page

September 10, 2007: Linux Hints, Tips, and Tricks

The document discusses various ways to perform calculations and conversions in Linux scripts and at the command line using tools like (( )), bc, and units. Simple math can be done with (( )), floating point with bc, and hex conversions between decimal and hex are possible with (( )) or bc. The units command allows converting between units like miles and km or looking up definitions.

Uploaded by

arunabhatla
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views1 page

September 10, 2007: Linux Hints, Tips, and Tricks

The document discusses various ways to perform calculations and conversions in Linux scripts and at the command line using tools like (( )), bc, and units. Simple math can be done with (( )), floating point with bc, and hex conversions between decimal and hex are possible with (( )) or bc. The units command allows converting between units like miles and km or looking up definitions.

Uploaded by

arunabhatla
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

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)

You might also like