Simple Bash Script to Reverse Your Name and Output it

Just a quick script to reverse some input, using Bash and the FOSS "rev" program. It's amazing how easy it is to manipulate things with Bash. I love it!


Bash script version:
#!/bin/bash
#Simple script to reverse an input name

echo "Enter your name";read 'myname'


echo "Your name spelled backwards is: $( echo $myname | rev )"

exit 0


One-liner version:
echo -n "what is your name?";read name;echo "$name" |rev


Or how about this more ridiculous example (one that doesn't use the "rev" program):
#!/bin/bash
#Simple script to reverse an input name


  echo -n "Enter your name:"
  read myname

  numChars=$(echo -n "$myname" |wc -c)
  revname=
  while [ $numChars -gt 0 ]
  do

      revname=$revname$(echo -n "$myname"|cut -b$numChars)
      numChars=$(expr $numChars - 1)

 done

echo "Your name spelled backwards is: $revname"

exit 0


Ridiculous one-liner version (one that doesn't use the 'rev' program):
echo -n "Enter your name:";read myname;numChars=$(echo -n "$myname" |wc -c);revname=;while [ $numChars -gt 0 ];do revname=$revname$(echo -n "$myname"|cut -b$numChars);numChars=$(expr $numChars - 1);done;echo "$revname"


Comments

Popular posts from this blog

On Helping Others Get their GNU/Linux & Consider Doing So

How To: Install Wordpress on Manjaro Linux Pahvo 21.1.0

How to Disable Middle Mouse Button in Ubuntu / Debian Linux