Open In App

Reverse a String | Shell Programming

Last Updated : 18 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In shell scripting, reversing a string can be done using various methods, such as rev, awk, sed, and Perl. Here, we provide examples of reversing a string using different approaches and commands in Unix/Linux systems. We are given a string and we have to use shell script to print it in the reverse order.

Syntax

The general syntax varies depending on the command used for reversing the string.

Using rev command:

echo "string" | rev

Using awk command:

echo "string" | awk '{ for(i = length; i!=0; i--) x = x substr($0, i, 1);} END {print x}'

Using sed and tac:

echo "string" | sed 's/./&\n/g' | tac | sed -e :a -e 'N;s/\n//g;ta'

Using perl:

echo "string" | perl -ne 'chomp; print scalar reverse;'

Key Methods for Reversing Strings

Asked in FICO Examples:

Input : geeksforgeeks 
Output :skeegrofskeeg

Algorithm

step 1:- Take user input in a string
step 2:- Findthe length of given string using length function
step 3:- Set i = length-1 and run loop till i <= 0
step 4:- echo the $i
step 5:- repeat step 3 and 4 till i==0
step 6:- end
CPP
// reverse a string using shell script
// reverse a string is in linux and unix

#!/ bin / bash
// reading a string
// using via user input
read - p &quot;Enter string:&quot; string
    // getting the length of given string
    len
    = $
{
    #string
}
// looping for reversing a string
// initialize i=len-1 for reversing a string and run till i=0
// printing in the reverse order of the given string
for ((i = $len - 1; i &gt;= 0; i--))
    do
    // &quot;${string:$i:1}&quot;extract single character from string.
    reverse = &quot;$reverse${string:$i:1}&quot; done
        echo &quot;$reverse&quot;

Output:

skeegrofskeeg

Reverse a string in shell scripting using Commands

Method 1: Using rev Command

The rev command reverses the lines in a file or from standard input.

rev command: It is used to reverse the lines in a file. This command can take standard input as well as shown below.

$ echo welcome | rev
emoclew

Note: The rev command is not present in all flavors of Unix.

Method 2: Using awk Command

Using awk to reverse a string by iterating over each character from the end and appending it.

awk command: Using the substring function, can reverse a string

$ echo welcome | awk 
'{ for(i = length; i!=0; i--)
x = x substr($0, i, 1);
}END
{print x}'

emoclew

This logic is the common logic used in any programming language to reverse a string: Start from the last position of the string and keep printing a character till the first position is reached. We loop on the string starting from the last position and moving towards the beginning.

The length command gives the length of the argument passed to it. With no argument, length gives the length of the current line which is $0. The substr command in awk extracts one character at a time and is appended to the resultant variable x which is printed at the end using the END label.

Method 3: Using sed and tac Commands

This method uses sed to append a newline after every character, tac to reverse the lines, and sed again to join them.

sed command: Total 2 sed commands are used here.

$ echo welcome | sed 's/./&\n/g' | tac | sed -e :a -e 'N;s/\n//g;ta'
emoclew

To understand this command properly, break it up at every pipe and observe the output. The first sed reads a character(.) and appends a newline(\n) character behind every matched character(&). tac command reverses the contents of a file or the standard input. The second sed command now joins all the lines together. Note: The tac command is not present in all the Unix flavors.

Method 4: Using perl

The perl solution uses the built-in reverse function to reverse the string.

Perl solution: Using the inbuilt perl function reverse.

$ echo welcome | perl -ne 'chomp;
print scalar reverse;'
emoclew

reverse function reverses all the elements present in a list. In scalar context as used here, it reverses a string as well. The chomp command is used to remove the newline character in the string.

Conclusion

Reversing a string in shell programming can be done using a variety of commands such as rev, awk, sed, or perl. Each method has its own use cases, with some commands being more readily available on certain systems.


Next Article
Article Tags :

Similar Reads