Bash can seem pretty random and weird at times, but most of what people see as quirks have very logical (if not very good) explanations behind them. This series of posts looks at some of them.
Let’s say you wanted to enforce a policy in which no files on the system could contain swearing. How would you write a script that checks it? Let’s use the word “damn”, and let’s write a script “checklanguage” that checks whether a file contains that word.
Our first version might be:
#!/usr/bin/env bash grep -q "damn" "$@"
The problem with this is that it triggers on itself: ./checklanguage checklanguage
returns true. How can we write the script in such a way that it reliably detects the word, but doesn’t detect itself? (Think about it for a second).
There are many ways of doing this: a="da"; b="mn"; grep "$a$b"
, grep "da""mn"
, grep da\mn
. All of these check for the four characters d-a-m-n in sequence, but doesn’t contain the sequence itself. These methods rely on two things being A. identical in one context (shell script) and B. different in another (plaintext).
This type of trick is the basis of three common command line hacks:
Finding processes from ps, while excluding the grep that does the filtering.
If we do a simple ps ax | grep processname
, we might get output like this:
$ ps ax | grep processname 13003 pts/2 S 0:00 /bin/bash ./processname 13496 pts/4 R+ 0:00 grep --color=auto processname
How do we get the same list, but without the grep process? You’ll see people wrapping the first character in square brackets:
$ ps ax | grep "[p]rocessname" 13003 pts/2 S 0:00 /bin/bash ./processname
In this case, the regex “[p]rocessname” is identical to the regex “processname”, but since they’re written differently, the latter matches itself while the former doesn’t. This means that the grep won’t match itself, and we only get the process we’re interested in (this job is better done by pgrep
).
There is no syntax rule that says “if the first character is enclosed in square brackets, grep shall ignore itself in ps output”.
It’s just a logical side effect of rewriting the regex to work the same but not match itself. We could have used grep -E 'process()name'
or grep -E 'proces{2}name'
instead.
Running commands instead of aliases
Maybe you’re sick of Debian’s weird perl rename, and you aliased it to rename.ul
instead.
$ rename -v .htm .html * `foo.htm' -> `foo.html'
Yay, that’s way easier than writing regex! But what if we need to use the unaliased rename?
$ rename -v 's/([1-9])x([0-9]*)/S$1E$2/' * rename.ul: not enough arguments
Instead, you’ll see people prefixing the command with a backslash:
$ \rename -v 's/([1-9])x([0-9]*)/S0$1E$2/' * Foo_1x20.mkv renamed as Foo_S01E20.mkv
Shell aliases trigger when a command starts with a word. However, if the command starts with something that expands into a word, alias expansion does not apply. This allows us to use e.g. \ls
or \git
to run the command instead of the alias.
There is no syntax rule that says that “if a command is preceded by a backslash, alias expansion is ignored”.
It’s just a logical side effect of rewriting the command to work the same, but not start with a literal token that the shell will recognize as an alias. We could also have used l\s
or 'ls'
.
Deleting files starting with a dash
How would you go about deleting a file that starts with a dash?
$ rm -v -file rm: invalid option -- 'l'
Instead, you’ll see people prefixing the filename with ./
:
$ rm -v ./-file removed `./-file'
A command will interpret anything that starts with a dash as a flag. However, to the file system, -file
and ./-file
mean exactly the same thing.
There is no syntax rule that says that “if an argument starts with ./, it shall be interpretted as a filename and not an option”.
It’s just a logical side effect of rewriting a filename to refer to the same file, but start with a different character. We could have used rm /home/me/-file
or rm ../me/-file
instead.
Homework: What do you tell someone who thinks that ./myscript
is a perfect example of how weird UNIX is? Why would anyone design a system where the run command is “./” instead of “run”?