A bash calculator

From SHellium Wiki
Jump to: navigation, search
Geographylogo.png In other languages: English | Afrikaans | Albanian | Arabic | Brazilian | Bulgarian | Catalan | Chinese | Croatian | Czech | Danish | Dutch | Esperanto | Estonian | Filipino | Finnish | Flemish | French | German | Greek | Hebrew | Hindi | Hungarian | Indonesian | Italian | Japanese | Latvian | Lithuanian | Macedonian | Malay | Malayalam | Norwegian (Bokmål) | Norwegian (Nynorsk) | Persian | Polish | Portuguese | Romanian | Russian | Serbian | Slovak | Slovenian | Spanish | Swedish | Turkish | Ukrainian | Urdu

A bash calculator

This is simple bash trick for quick and dirty arithmetic calculation. Here, we make use of bc which is by definition is an arbitrary precision calculator language and quite a flexible tool. What we are about make is a shell calculator which takes standard arithmetic expressions as input and provides us with the required calculated output.

Now, in your ~/.bashrc file punch in following function. In the example below, the switch -l makes bc use the standard math library. The parameter "${1}" is echoed and piped to bc.

function calcu
 {
   echo "${1}"|bc -l;
 }

Reload your .bashrc file

source ./.bashrc

Example calculation

calcu 14+5/2*6
29.00000000000000000000
Personal tools
Namespaces

Variants
Actions
Navigation
Indexes
SHellium Sites
Toolbox