In the 1990s, it was pretty common to hide certain text using a special “cipher” called rot13. However, this wasn’t really encryption; instead, it was just a way to hide non-sensitive text so its meaning wouldn’t be immediately obvious. A common use was to hide spoilers for TV shows and movies, or to give the answer to a (dumb) joke:
A clumsy paleontologist walked into an auditorium carrying a dinosaur skull.
Jung unccrarq arkg vf wnj qebccvat.
One way to un-hide these messages is to write a program that rotates the letters in a message by 13 positions in the alphabet. Because English has 26 letters, rotation by 13 positions will hide the message, and rotation by another 13 positions will reveal the message.
Such a program doesn’t need to be very long; a sample implementation is only about 26 lines long. However, you can actually do it via the command line, without writing a program.
Translating characters
The tr program translates characters from one set to another. In the simplest case, you can use tr to translate the letter A to the letter B, like this:
$ echo ABC | tr A B
BBC
The tr program also supports ranges of letters. Lets say you wanted to translate all uppercase letters to become lowercase letters. You would specify A–Z as the letters to translate from and a–z as the letters to translate to, such as this command line:
$ echo Hello World | tr A-Z a-z
hello world
You can also specify multiple ranges to translate from and to, as long as both sets are the same “size.” For example, to translate only the letters A–C to become the letters X–Z, and the numbers 1–3 to the numbers 7–9, you can just list both ranges on the same command line:
$ echo Blue300 | tr A-C1-3 X-Z7-9
Ylue900
Hiding text
Using this method, we can apply tr to “rotate” letters in a message by 13 positions. That is, the letters A–N and a–n become M–Z and m–z, and the letters M–Z and m–z become A–N and a–n. The tr command line starts to look a little weird, but it is a valid way to specify multiple ranges on the command line:
$ echo Hello World | tr A-Ma-mN-Zn-z N-Zn-zA-Ma-m
Uryyb Jbeyq
Applying the tr command a second time reveals the original message:
$ echo Hello World | tr A-Ma-mN-Zn-z N-Zn-zA-Ma-m | tr A-Ma-mN-Zn-z N-Zn-zA-Ma-m
Hello World
I find this is easiest to use if I put the tr command in a shell script, so I can use it whenever I need it. This is essentially a “wrapper” to the tr command; it only runs the command and doesn’t do anything else:
#!/bin/sh
tr A-Ma-mN-Zn-z N-Zn-zA-Ma-m
Because the shell script only runs the tr command and doesn’t rely only any specific Bash features, this can run under any shell. That’s why I’ve used /bin/sh (the original Bourne shell), although in practice this is almost guaranteed to be a link to the Bash shell anyway.