Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.
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.

share|improve this question
1  
*\ is not a valid arithmetic operator in bash. What is your expected input and output? –  Chris Down Feb 24 '13 at 6:27

5 Answers 5

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
share|improve this answer

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.

share|improve this answer

*\ 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.

share|improve this answer

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

share|improve this answer
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"
share|improve this answer
    
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. –  G-Man Jun 20 at 21:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.