23 November 2012

Shell Script to reverse a String

3 comments

Q: How do I reverse a string in Linux/Unix?


Ans:

#!/bin/bash
read -p "Enter string:" string
len=${#string}
for (( i=$len-1; i>=0; i-- ))
do
# "${string:$i:1}"extract single single character from string.
reverse="$reverse${string:$i:1}"
done
echo "$reverse"

Output


how to reverse a string in bash

Enjoy :)


If You Liked This Post Please Take a Time To Share This Post

You May Also Like...

3 comments:

  1. Thanks sir, for publish this shell script.

    ReplyDelete
  2. Can you explain me the script. I am not able to undestand.

    ReplyDelete
    Replies
    1. TUMSE NA HO PAYEGA BETA...

      Delete