HowTo: Reverse a String In Unix / Linux Shell?

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.

ADVERTISEMENTS

Say hello to rev command

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 "nixcraft" | rev

Sample outputs:

tfarcxin

You can use the following syntax too:

rev<<<"This is a test"

Sample outputs:

tset a si sihT

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
input="$1"
reverse=""
 
len=${#input}
for (( i=$len-1; i>=0; i-- ))
do 
	reverse="$reverse${input:$i:1}"
done
 
echo "$reverse"

Run it as follows:
./script nixcraft
Sample outputs:

tfarcxin
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
6 comments… add one
  • whyqaz Jul 9, 2012 @ 9:16

    What does ‘<<<' do?
    Or what is it?

  • Paulo Freitas Jul 9, 2012 @ 17:58

    Python equivalent:

    echo foo | python -c 'import sys;print(sys.stdin.read().strip()[::-1])'
    python -c 'import sys;print(sys.stdin.read().strip()[::-1])' <<< foo
    

    PHP equivalent:

    echo foo | php -r 'print strrev(trim(fgets(STDIN)));'
    php -r 'print strrev(trim(fgets(STDIN)));' <<< foo
    
  • 🐧 nixCraft Jul 9, 2012 @ 21:06

    @whyqaz
    See Here strings.

    Hope this helps!

  • Radha Oct 4, 2013 @ 15:54

    I am looking to reverse 4 digits in a string, could you please let me know what would be syntax, and data will be 178126*********4321 , it is not a column, so it possible if we can identify the position of charcter like 12 th to 16 charecter needs to be reveresed , is it possible, if yes could you please let me know the syntax.

    Thanks
    Radha

    • foobar Feb 25, 2015 @ 13:48

      Can be done using awk split function. Example reversing the three middle characters of a string:

      echo 1234567 | awk '{split($1,a,"");printf("%s%s%s%s%s%s%s",a[1],a[2],a[5],a[4],a[3],a[6],a[7])}'
  • Ray Ballinger Oct 13, 2015 @ 21:16


    Reverse it; i.e., make it point Left.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.