Reverse multiword

Just check out the script...

 
      1 #!/bin/bash
      2
      3 echo -n "Enter a string :: "
      4 read str
      5 echo -n "Reverse is :: "
      6 l=`expr length "$str"`
      7 while [ $l -ge 1 ]
      8 do
      9     m=`echo $str | cut -c "$l"`
     10     echo -n $m
     11     l=`expr $l - 1`
     12 done
     13 echo

It takes a script and shows it in reverse manner.

it works well for single word input like..

But ignores the white space if multi-word is entered...

How can I fix this ??

Use $m in dquotes

echo -n "$m"

Here is the code

amit@softpc142108 ars $ cat rev.sh
#!/bin/bash
echo -n "Enter a string :: "
read str


echo $str | awk '{
for(i=length($0);i>=1;i--)
printf("%s",substr($0,i,1));
}'

amit@softpc142108 ars $

Check out the command
$rev file.txt
which is going to reverse the line.