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:
1This is a line of text
2txet fo enil a si siht
3another oneand rev is supplied example.txt, then the output will showcase the reversed result.
1% rev example.txt
2txet fo enil a si sihT
3this is a line of text
4eno rehtonarev with standard input
rev accepts standard input by default.
1% rev
2this is a line
3enil a si sihtOR
1% echo this is a line | rev
2enil a si sihtThis is useful for scripting capabilities:
1foo="I want this text reversed"
2bar=$(echo $foo | rev)
3echo $bar