23 November 2012

Shell Script to reverse a String

6 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...

6 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
    2. mujse hopayenga ullukkapattttaaaaaa
      kameene

      Delete
  3. shell script wrong... in line 4: if i include the first line
    "#!/bin/bash"

    it shows :
    line 4: ((: i=#3-1: syntax error: operand expected (error token is "#3-1")

    ReplyDelete
  4. wrong script
    mistake in line4
    bloody fool

    ReplyDelete