rev command in Linux and how to reverse strings

What is the rev command in Linux?

What is the rev command?

As the name implies, the rev command (short for reverse) allows users to reverse a line of characters or a file.

How is the rev command used?

The rev command is typically supplied a string of characters or a file input.

rev with a file

If a file named example.txt contains:

1
2
3
This is a line of text
txet fo enil a si siht
another one

and rev is supplied example.txt, then the output will showcase the reversed result.

1
2
3
4
% rev example.txt
txet fo enil a si sihT
this is a line of text
eno rehtona

rev with standard input

rev accepts standard input by default.

1
2
3
% rev
this is a line
enil a si siht

OR

1
2
% echo this is a line | rev
enil a si siht

This is useful for scripting capabilities:

1
2
3
foo="I want this text reversed"
bar=$(echo $foo | rev)
echo $bar