Friday, November 9, 2012

Shell: How to increment a value in a shell script

This is a very basic question that comes into our mind many times while writing a shell script.

Here is a small code that you may find it useful.

#!/bin/sh
i=0
j=5
while [ $i -le $j ]
do
i=`expr $i + 1`
echo $i
done

Here I am using expr unix command. This command evaluates arguments as expressions. You can find more details about expr by typing man expr in your terminal or check this link for online manual.

No comments:

Post a Comment