Thursday, February 21, 2008

Fun with bash - Addition at bash prompt

Addition at bash prompt can be done in many ways. I have managed to collate some of them in the script below. The script below counts from 1 to 14 in 14 different ways. Its fun isn't it. It shows how powerful and feature-rich this simple scripting language is.

#!/bin/bash
n=1;
echo -n "$n, "
let "n = $n + 1"
echo -n "$n, "
: $((n = $n + 1))
echo -n "$n, "
(( n = n + 1 ))
echo -n "$n, "
n=$(($n + 1))
echo -n "$n, "
: $[ n = $n + 1 ]
echo -n "$n, "
n=$[ $n + 1 ]
echo -n "$n, "
let "n++"
echo -n "$n, "
(( n++ ))
echo -n "$n, "
: $(( n++ ))
echo -n "$n, "
: $[ n++ ]
echo -n "$n, "
n=`echo "$n + 1" | bc`
echo -n "$n, "
n=`echo "$n + 1" | bc -l`
echo -n "$n, "
n=`expr $n + 1`
echo "$n"

No comments: