4
clear
echo "Enter a number"
read n
sd=0
rev=0

while [ $n -gt 0 ]
do
    sd=$(( $n % 10 ))
    rev=$(( $rev *\ 10 + $sd ))
    n=$(( $n / 10 ))
done

echo "Reverse number of entered digit is $rev"

From the above code I am unable to get the required output. Instead, this error is displayed.

./Display: line 17: 0 *\ 10 + 4 : syntax error: operand expected (error token is "\ 10 + 4 ")
Reverse number of entered digit is 0

I don't know why this error is displayed, please help me to figure it out.

1
  • 2
    *\ is not a valid arithmetic operator in bash. What is your expected input and output?
    – Chris Down
    Commented Feb 24, 2013 at 6:27

8 Answers 8

18

Numbers are text too. Text can be reversed with rev without any arythmetic.

#!/bin/bash
clear
read -p "Enter a number: " num
echo $num | rev
3

The error had already pointed out the problem clearly: 0 *\ 10 + 4 isn't a valid arithmetic expression.

I believe *\ is a typo for *. Fix this typo and the script will be OK.

1
  • \* is advised notation for arithmetic multiplication as shell is performing expansion within the (( )) wchich may lead to * being interpreted as a wildcard.
    – nethero
    Commented Oct 25, 2020 at 11:53
1

*\ is not the valid expression. Instead, try * which removes the special meaning of the character *. The character * is a wildcard character, which is why the error appeared.

1
echo "Enter a number"
read n
sd=0
rev=0

while [ $n -gt 0 ]
do
    sd=$(( $n % 10 ))
    rev=`expr $rev \* 10 + $sd` 
    n=$(( $n / 10 ))
done

echo "Reverse number of entered digit is $rev"
1
  • Welcome to Stack Exchange. It would be great if you could find the time to edit your answer to explain how this works, so that others can benefit in the future. (You're more likely to get +1 votes if you do.) Commented Oct 3, 2015 at 21:29
0

* is a wildcard character. Hence make sure that you add a '\' before using it

0
echo "Enter a number"
read n
sd=0
rev=0

while [ $n -gt 0 ]
do
    sd=$(( $n % 10 ))
    rev=$(( $rev * 10 + $sd ))
    n=$(( $n / 10 ))
done

echo "Reverse number of entered digit is $rev"

This should work *\ is not the correct form just use * for this

1
  • Although the code is appreciated, it should always have an accompanying explanation. This doesn't have to be long, but it is expected.
    – peterh
    Commented Oct 13, 2015 at 13:35
-1
echo "enter the number"
read n
rev=0
sd=0
while [$n -gt 0]
do
    sd=`expr $n%10`
    rev=`expr $rev\*10+$sd`
    n=`expr $n/10`
done
echo "the reverse number s: $rev"
1
  • Welcome to Stack Exchange.  Did you try this?  Did it work?  If so, please describe your system, because this won't work on any normal system.  While you seem to be somewhat on the path to a solution (script) that does what the OP was trying to do, it's a bad way to do it.  And even if it did work, it would be a better answer if you could provide some explanation why it does so.  Answers that don’t include explanations may be removed. Commented Jun 20, 2015 at 21:41
-2
for i in $*; do
  x=$i"  "$x
done;
echo "The Reverse order is $x"
1
  • Unless I am mistaken, on most patched versions of Bash, $x can no longer be set as a variable. This is also a horrible answer. Really bad answer. Opposite of good. Just bad. Commented Nov 28, 2015 at 13:34

Not the answer you're looking for? Browse other questions tagged .