Shell tidbits- 3 mins
This is the first of a long (maybe) series of blog posts with some less known shell features. Unless otherwise specified, bash is the shell of reference.
Calling this encryption is a bit unpolite, but let’s do it, we don’t care right?
ROT13 ("rotate by 13 places", sometimes hyphenated ROT-13) is a simple letter substitution cipher that replaces a letter with the 13th letter after it, in the alphabet.
I use this, sometimes, just to make things a bit arcane for those n00bs not looking enough far, and to pass sensible, obfuscated data from one script to another.
To ROT13 a string we can use tr (translate or delete characters), so I wrote a simple function that take a string as input and output its ROT13 permutation.
For example, let’s ROT13 and back an “Hello World”
[tmow@Ade ~]$ echo "$(rot13 "Hello World")" Uryyb Jbeyq [tmow@Ade ~]$ echo "$(rot13 "Uryyb Jbeyq")" Hello World [tmow@Ade ~]$
Don’t use this to store passwords as it’s not safe at all (exept against idiots).
Redefine a command
What if you need to execute a script on different unixes with different implementations of, let’s say, awk and sed?
Aliases cannot be easily used inside a script, so often people set an AWK and SED variable and than use that varbiale inside the script, instead of calling directly the awk/sed variant.
I really don’t like “executing” variables and I don’t like to ignore shellcheck exceptions.
So, the most elegant way I’ve found is to redefine a command with a function.
Functions are interepted before programs in the PATH environment variable, therefore if you are on SunOS, when you will call awk, bash will call the function awk and not the awk command.
Variable is defined
Since bash v4.2, we can test if a variable is set with
Before these days and on Unices where bash >4.2 it’s a dream, we can use typeset (or declare).
I normally use this in a function that I use every time I need to test if a variable is defined.
That I can use this way:
On some Posix unix, there is not yet basename, you can obtain the same result with just a printf and a the bash strings operations.
Same as basename…