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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
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
2  
*\ is not a valid arithmetic operator in bash. What is your expected input and output? – Chris Down Feb 24 '13 at 6:27

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 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"
share|improve this answer
    
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.) – roaima Oct 3 '15 at 21:29
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

share|improve this answer
    
Although the code is appreciated, it should always have an accompanying explanation. This doesn't have to be long, but it is expected. – peterh Oct 13 '15 at 13:35
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 '15 at 21:41
for i in $*; do
  x=$i"  "$x
done;
echo "The Reverse order is $x"
share|improve this answer
    
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. – rubynorails Nov 28 '15 at 13:34

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.