I am writing the script to print multiplication table.
#!/bin/bash
a=1
while [ $a -le "10" ]
do
tmp=`expr $a * $1`
printf "%d x %d = %d\n" $1 $a $tmp
a=`expr $a + 1`
done
It gives syntactical error.
Escape * as following
while [ $a -le "10" ]
do
tmp=`expr $a \* $1`
printf "%d x %2d = %3d\n" $1 $a $tmp
a=`expr $a + 1`
done
Plz note \* in above code.
Here bash interprets * as wild character. So you need to escape it to literal star(i.e multiplication. If you dont want to escape * then you can use (( )) which performs arithematic operations.
while [ $a -le "10" ]
do
((tmp = $a * $1))
printf "%d x %2d = %3d\n" $1 $a $tmp
((a++))
done
expror backticks unless you're writing for an ancient non-POSIX bourne shell. If you're using a bash shebang, don't use[for math either.