DT Logo
/ Blog
/ 2023
/ 12
/ 28
/ The Command Line is a Gun

The Command Line is a Gun

Diving into the terminal, you will find that lines of command share a surprising kinship with firearm safety. Picture this: every keystroke is a potential bullet, and just as a responsible shooter takes deliberate aim, developers and administrators can adopt a similar mindset to safeguard their digital landscapes. Let's explore the unexpected parallels between command craftsmanship and muzzle discipline, uncovering valuable lessons that go beyond the screen and into the realm of responsible coding practices

Imagine a situation: you're working on a server when disk space starts going low. You check what's using it and find out some directory in /tmp has been accumulating junk. All right, let's clear it out.

rm: cannot to remove '/tmp/junk': Permission denied

All right, think carefully about what you would do in this situation. What do you need to type?

...

Obviously, it's sudo rm -rf /tmp/junk.

But what's actually important is how you type, not what you type.

Firearm rule: Flagging

In firearm safety, there's a general rule that you don't point the muzzle at anything you don't intend to completely destroy. If you accidentally do at, some point, have your weapon aimed in an unsafe direction (e.g. at a friendly person) that's called flagging. In this post, I propose that typing out dangerous (i.e. irreversible commands) in the traditional fashion (i.e. left-to-right) is the command-line equivalent of 'flagging'.

Whilst typing sudo rm -rf /tmp/junk, there is a brief moment where what you have typed is sudo rm -rf /, which for the uninitiated, nukes your computer. Now while you'll carry on typing just fine probably 99.9% of the time, who knows what freak accident might happen? Maybe your pinky taps the ENTER key too early--maybe your cat jumps onto your keyboard. Uh oh. So here's what I propose as a habit to do instead: type everything without sudo first (rm -rf /tmp/junk), and write it back in once the command has been constructed. For extra added safety, you could also inspect the target file or directory beforehand--this would be the equivalent to confirming that what you're pointing your weapon at is positively what you want to destroy.

This isn't limited to linux shells either.

Firearm rule: The "Safety"

Consider SQL commands (particularly UPDATE and DELETE). As a contrived example:

sql
    DELETE FROM users WHERE name IS 'john' AND email ILIKE '%john%@example.com';

    Some tools, like SQL, have a benefit that system shells typically don't have, which is the requirement of the semi-colon at the end--this is equivalent to the safety on a gun. However, firearm safeties should also not be relied upon as an infallible defense against negligent discharges. In this case, prior to completing your WHERE filters, you essentially have a query threatening to nuke your table. In addition, even if you type the full query out, there's still the risk of having an inaccurate WHERE filter. For this, I propose constructing your query initially with SELECT instead. The structure of the query remains exactly the same, you don't flag the entirety of your table, and at the end you can get confirmation that the rows you have selected are indeed the rows you are trying to delete.

    Conclusion

    There's a surprising amount out of parallels you can draw between command-line and firearm practices. The two I covered here seem to be the most applicable. There's a lot of additional layers of safety practices you can employ, but I feel like these are the bare minimum you should do in order to not have an incident. Some people might say this is too tedious, but to me it just seems like it should be common sense--and common practice.

    Current visitors: 1