13.6 C
Jaipur
Sunday, January 23, 2022

How To Reverse a String In Unix / Linux Shell?

Must read

How do I reverse lines of a file or files under Linux or Unix like operating systems?

You can use any one of the following method to reverse a string in Linux or Unix shell.

Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux or Unix
Est. reading time 1 minute

How do I reverse a string in Linux and Unix?

The procedure and commands are as follows for reversing strings or words:

  1. Reverse lines and words characterwise using the rev command
  2. Use perl with print scalar reverse option.
  3. One can use the awk command to reverse data under Linux or Unix.
  4. There is also sed command one liner for reversing given input
  5. Finally, one can write a shell script to reverse input one-by-one

Let us see all options and commands in details.

Patreon supporters only guides 🤓

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare
  • Join my Patreon to support independent content creators and start reading latest guides:

Join Patreon

Say hello to the rev command to reverse lines characterwise

The rev command copies the specified files, reversing the order of characters in every line. If no files are specified, the standard input (from keyboard) is read. If rev command is installed use it as follows:

echo "string" | rev
echo "nixcraft" | rev

You can use the following syntax too:

rev <<<"string"
rev <<<"Hello World"
rev<<<"This is a test"
# let us reverse euler's number  #
echo "2.71828" | rev

Here is how reverse string looks:

Reverse a string bash shell scripting under Linux and Unix

You can store output a shell variable as follows for processing purposes:

var=$(rev<<<"input_data_here")
another_var="Every moment is a fresh beginning"
output=$(rev<<<"${another_var}")

Then display it using the printf command or echo command:

printf "%sn" "$var"
echo "Original: $another_var"
echo "$output"

Reversing a string in shell

A note about dealing with a new line (n)

The following example will fail:

echo -e 'FoonBar' | rev
echo -e '42n13' | rev

To get around this, try the tac command or the tail command to print last line first. For example:

echo -e 'FoonBar' | rev | tac
echo -e '42n13' | rev | tac
#
# macOS and *BSD users try the gtac (GNU tac) or tail -r 
#
echo -e '42n13' | rev | tail -r
rev <<<$'42n13' | tail -r
rev <<<$'42n13' | gtac

Bash reversing the order of characters in a string (multiline input)

Perl script example to reverse a string

You can use the perl and syntax is:

perl -ne 'chomp;print scalar reverse . "n";'<<<"nixcraft"

OR

echo 'nixcraft' | perl -ne 'chomp;print scalar reverse . "n";'

Bash shell script example to reverse a string

You can use bash and sample bash script is:

#!/bin/bash
set -euo pipefail
input="${1:?Error input missing}"
reverse=""
 
len=${#input}
for (( i=$len-1; i>=0; i-- ))
do 
	reverse="$reverse${input:$i:1}"
done
 
echo "Input (original): $input"
echo "Output (rev): $reverse"

Run the .sh file shell script as follows:
./script nixcraft

Summing up

You learnt how to reverse a string in Bash or KSH or any other modern shell when using macOS, Linux, *BSD or Unix-like systems.


ADVERTISEMENT


Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -

Latest article