Skip to main content
updated syntax for newer versions of fish
Source Link
ndemou
  • 3k
  • 2
  • 24
  • 27

For Integer arithmetic (where 3/2=1)

  • bash echo $(( 1+1 ))
  • fish math 1+1
  • zsh* echo $((1+1))

*: and ksh93, yash

For floating point arithmetic (where 3/2=1.5)

  • bash awk "BEGIN {print 10/3}" (low precision)
  • bash echo "10/3"|bc -l (high precision)
  • fish math "scale=4;-s4 10/3"3
  • zsh* echo $((10./3))

*: and ksh93, yash

You can of course configure your shell to use awk with minimum typing like calc 10/3 (see notes on how to do it for bash1 and fish2).

The main reason for suggesting awk for bash is that it's preinstalled on almost all Unix-like OSes and is reasonably light (there is of course the cost of starting a process) with a less precise but more human-friendly output than bc -l which prints 20 decimal digits (although you can certainly tweak awk to get more decimal digits).


##Notes##

(1) How to use the simplified syntax in bash###

Add this bash function to your ~/.bashrc:

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

(2) How to use the simplified syntax in fish###

Create a calc fish function (i.e. a text file named /home/ndemou/.config/fish/functions/calc.fish):

function calc
    awk "BEGIN{ print $argv }" ;
end

For Integer arithmetic (where 3/2=1)

  • bash echo $(( 1+1 ))
  • fish math 1+1
  • zsh* echo $((1+1))

*: and ksh93, yash

For floating point arithmetic (where 3/2=1.5)

  • bash awk "BEGIN {print 10/3}" (low precision)
  • bash echo "10/3"|bc -l (high precision)
  • fish math "scale=4; 10/3"
  • zsh* echo $((10./3))

*: and ksh93, yash

You can of course configure your shell to use awk with minimum typing like calc 10/3 (see notes on how to do it for bash1 and fish2).

The main reason for suggesting awk for bash is that it's preinstalled on almost all Unix-like OSes and is reasonably light (there is of course the cost of starting a process) with a less precise but more human-friendly output than bc -l which prints 20 decimal digits (although you can certainly tweak awk to get more decimal digits).


##Notes##

(1) How to use the simplified syntax in bash###

Add this bash function to your ~/.bashrc:

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

(2) How to use the simplified syntax in fish###

Create a calc fish function (i.e. a text file named /home/ndemou/.config/fish/functions/calc.fish):

function calc
    awk "BEGIN{ print $argv }" ;
end

For Integer arithmetic (where 3/2=1)

  • bash echo $(( 1+1 ))
  • fish math 1+1
  • zsh* echo $((1+1))

*: and ksh93, yash

For floating point arithmetic (where 3/2=1.5)

  • bash awk "BEGIN {print 10/3}" (low precision)
  • bash echo "10/3"|bc -l (high precision)
  • fish math -s4 10/3
  • zsh* echo $((10./3))

*: and ksh93, yash

You can of course configure your shell to use awk with minimum typing like calc 10/3 (see notes on how to do it for bash1 and fish2).

The main reason for suggesting awk for bash is that it's preinstalled on almost all Unix-like OSes and is reasonably light (there is of course the cost of starting a process) with a less precise but more human-friendly output than bc -l which prints 20 decimal digits (although you can certainly tweak awk to get more decimal digits).


##Notes##

(1) How to use the simplified syntax in bash###

Add this bash function to your ~/.bashrc:

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

(2) How to use the simplified syntax in fish###

Create a calc fish function (i.e. a text file named /home/ndemou/.config/fish/functions/calc.fish):

function calc
    awk "BEGIN{ print $argv }" ;
end
Corrected silly math error :-)
Source Link
ndemou
  • 3k
  • 2
  • 24
  • 27

For Integer arithmetic (where 3/2=1)

  • bash echo $(( 1+1 ))
  • fish math 1+1
  • zsh* echo $((1+1))

*: and ksh93, yash

For floating point arithmetic (where 3/2=1.5)

  • bash awk "BEGIN {print 10/3}" (low precision)
  • bash echo "10/3"|bc -l (high precision)
  • fish math "scale=4; 10/3"
  • zsh* echo $((10./3))

*: and ksh93, yash

You can of course configure your shell to use awk with minimum typing like calc 10/3 (see notes on how to do it for bash1 and fish2).

The main reason for suggesting awk for bash is that it's preinstalled on mostalmost all Unix-like OSes and is reasonably light (there is of course the cost of starting an externala process of course) with a less precise but more human-friendly output than bc -l which prints 20 decimal digits (although you need tocan certainly tweak itawk to get more decimal digits if you want to get more decimal digits).


##Notes##

(1) How to use the simplified syntax in bash###

Add this bash function to your ~/.bashrc:

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

(2) How to use the simplified syntax in fish###

Create a calc fish function (i.e. a text file named /home/ndemou/.config/fish/functions/calc.fish):

function calc
    awk "BEGIN{ print $argv }" ;
end

For Integer arithmetic (where 3/2=1)

  • bash echo $(( 1+1 ))
  • fish math 1+1
  • zsh* echo $((1+1))

*: and ksh93, yash

For floating point arithmetic (where 3/2=1.5)

  • bash awk "BEGIN {print 10/3}"
  • fish math "scale=4; 10/3"
  • zsh* echo $((10./3))

*: and ksh93, yash

You can of course configure your shell to use awk with minimum typing like calc 10/3 (see notes on how to do it for bash1 and fish2).

The main reason for suggesting awk is that it's preinstalled on most Unix-like OSes and is reasonably light (there is the cost of starting an external process of course) but you need to tweak it if you want to get more decimal digits.


##Notes##

(1) How to use the simplified syntax in bash###

Add this bash function to your ~/.bashrc:

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

(2) How to use the simplified syntax in fish###

Create a calc fish function (i.e. a text file named /home/ndemou/.config/fish/functions/calc.fish):

function calc
    awk "BEGIN{ print $argv }" ;
end

For Integer arithmetic (where 3/2=1)

  • bash echo $(( 1+1 ))
  • fish math 1+1
  • zsh* echo $((1+1))

*: and ksh93, yash

For floating point arithmetic (where 3/2=1.5)

  • bash awk "BEGIN {print 10/3}" (low precision)
  • bash echo "10/3"|bc -l (high precision)
  • fish math "scale=4; 10/3"
  • zsh* echo $((10./3))

*: and ksh93, yash

You can of course configure your shell to use awk with minimum typing like calc 10/3 (see notes on how to do it for bash1 and fish2).

The main reason for suggesting awk for bash is that it's preinstalled on almost all Unix-like OSes and is reasonably light (there is of course the cost of starting a process) with a less precise but more human-friendly output than bc -l which prints 20 decimal digits (although you can certainly tweak awk to get more decimal digits).


##Notes##

(1) How to use the simplified syntax in bash###

Add this bash function to your ~/.bashrc:

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

(2) How to use the simplified syntax in fish###

Create a calc fish function (i.e. a text file named /home/ndemou/.config/fish/functions/calc.fish):

function calc
    awk "BEGIN{ print $argv }" ;
end
Corrected silly math error :-)
Source Link
ndemou
  • 3k
  • 2
  • 24
  • 27

For Integer arithmetic (where 13/2=02=1)

  • bash echo $(( 1+1 ))
  • fish math 1+1
  • zsh* echo $((1+1))

*: and ksh93, yash

For floating point arithmetic (where 13/2=1.5)

  • bash awk "BEGIN {print 10/3}"
  • fish math "scale=4; 10/3"
  • zsh* echo $((10./3))

*: and ksh93, yash

You can of course configure your shell to use awk with minimum typing like calc 10/3 (see notes on how to do it for bash1 and fish2).

The main reason for suggesting awk is that it's preinstalled on most Unix-like OSes and is reasonably light (there is the cost of starting an external process of course) but you need to tweak it if you want to get more decimal digits.


##Notes##

(1) How to use the simplified syntax in bash###

Add this bash function to your ~/.bashrc:

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

(2) How to use the simplified syntax in fish###

Create a calc fish function (i.e. a text file named /home/ndemou/.config/fish/functions/calc.fish):

function calc
    awk "BEGIN{ print $argv }" ;
end

For Integer arithmetic (where 1/2=0)

  • bash echo $(( 1+1 ))
  • fish math 1+1
  • zsh* echo $((1+1))

*: and ksh93, yash

For floating point arithmetic (where 1/2=1.5)

  • bash awk "BEGIN {print 10/3}"
  • fish math "scale=4; 10/3"
  • zsh* echo $((10./3))

*: and ksh93, yash

You can of course configure your shell to use awk with minimum typing like calc 10/3 (see notes on how to do it for bash1 and fish2).

The main reason for suggesting awk is that it's preinstalled on most Unix-like OSes and is reasonably light (there is the cost of starting an external process of course) but you need to tweak it if you want to get more decimal digits.


##Notes##

(1) How to use the simplified syntax in bash###

Add this bash function to your ~/.bashrc:

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

(2) How to use the simplified syntax in fish###

Create a calc fish function (i.e. a text file named /home/ndemou/.config/fish/functions/calc.fish):

function calc
    awk "BEGIN{ print $argv }" ;
end

For Integer arithmetic (where 3/2=1)

  • bash echo $(( 1+1 ))
  • fish math 1+1
  • zsh* echo $((1+1))

*: and ksh93, yash

For floating point arithmetic (where 3/2=1.5)

  • bash awk "BEGIN {print 10/3}"
  • fish math "scale=4; 10/3"
  • zsh* echo $((10./3))

*: and ksh93, yash

You can of course configure your shell to use awk with minimum typing like calc 10/3 (see notes on how to do it for bash1 and fish2).

The main reason for suggesting awk is that it's preinstalled on most Unix-like OSes and is reasonably light (there is the cost of starting an external process of course) but you need to tweak it if you want to get more decimal digits.


##Notes##

(1) How to use the simplified syntax in bash###

Add this bash function to your ~/.bashrc:

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

(2) How to use the simplified syntax in fish###

Create a calc fish function (i.e. a text file named /home/ndemou/.config/fish/functions/calc.fish):

function calc
    awk "BEGIN{ print $argv }" ;
end
added 89 characters in body
Source Link
ndemou
  • 3k
  • 2
  • 24
  • 27
Loading
added 89 characters in body
Source Link
ndemou
  • 3k
  • 2
  • 24
  • 27
Loading
replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
Source Link
Loading
better formating, a bit of rewording, rearangment
Source Link
ndemou
  • 3k
  • 2
  • 24
  • 27
Loading
added 15 characters in body
Source Link
Stéphane Chazelas
  • 586.8k
  • 96
  • 1.1k
  • 1.7k
Loading
added 95 characters in body
Source Link
ndemou
  • 3k
  • 2
  • 24
  • 27
Loading
Source Link
ndemou
  • 3k
  • 2
  • 24
  • 27
Loading