Shell Script to Reverse a Number

shell script is a computer program designed to be run by the Unix shell, a command-line interpreter. The various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell scripts include file manipulation, program execution, and printing text. A script that sets up the environment, runs the program, and does any necessary cleanup, logging, etc. is called a wrapper.

In this post, we will write a shell script to reverse a number which is given as the input.

INPUT:
line 1: number

OUTPUT:
Print a single number which is the reverse of the given number

The following is the shell script to reverse a number which is given as the input:

echo enter n
read n
num=0
while [ $n -gt 0 ]
do
num=$(expr $num \* 10)
k=$(expr $n % 10)
num=$(expr $num + $k)
n=$(expr $n / 10)
done
echo number is $num

OUTPUT:

$ enter n
$ 456
$ number is 654

Let us know in the comments if you are having any questions regarding this shell script.

And if you found this post helpful, then please help us by sharing this post with your friends. Thank You

One comment

  1. what about echo $num |rev

Leave a Reply

%d bloggers like this: