Quantcast
Channel: linuxadmin: Expanding Linux SysAdmin knowledge
Viewing all articles
Browse latest Browse all 17842

bash command-line calculator

$
0
0

Ever want to be able to do basic arithmetic on the command line? You can use expr, but it only does integer math. Here's a bash function that will do the trick:

function calc () { awk "BEGIN { print $* ; }" } 

Add this to your .bash_profile, re-log in, and you'll be all set:

$ calc 1+1 2 $ calc 345/8835 0.0390492 

Complex statements need to be enclosed in quotes:

$ calc (1 + 15) / 2 -bash: syntax error near unexpected token `1' $ calc '(1 + 15) / 2' 8 

Edit: Also, remember to escape asterisks, otherwise you'll feed the function a file listing:

$ calc 2 * 2 22 $ calc 2 \* 2 4 
submitted by kooganani
[link] [23 comments]

Viewing all articles
Browse latest Browse all 17842

Trending Articles