3

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.

1
  • 2
    As an aside, don't use expr or 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. Commented Mar 12, 2013 at 4:56

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.