I recently posted on twitter asking for the best Bash tricks that most people don’t seem to know about, and I got a ton of great responses. There were a couple hundred tips, and I’ve collected my five favorite below.

Using Opterview, I’ve generated videos of each tip in action. For each tip, I did my best to credit everyone who mentioned it though with so many responses I might have missed a few. Please shoot me a note and let me know if that’s the case!

#1 Quickly Backup a Configuration File

How many times have you gone to edit a file, but were worried you wouldn’t be able to undo the changes? The common solution here is to create a copy of the file appending something like .backup to it.

The cool part is that Bash’s Brace Expansion feature means you don’t have to to type out the path to both files. If you wanted to backup your .bashrc file before editing it, you could create .bashrc.backup by running:

cp ~/.bashrc{,.backup}

Here’s a clip of me doing just that in an Opterview interview session:
brace expansion

Thanks to @neilkod, @shadowspar, @gilcreque, @nyanotech, and @PeterCMartini for this and and several other tips related to Brace Expansion.

#2 Re-run a Command with Sudo Without Retyping

It can be frustrating when you type out a long command, but it fails because it needs elevated permissions. Now you have to type it all over again. Next time this happens, you can run the command again with the necessary permissions by running:

sudo !!

Here’s me hitting just such a situation recreating a classic xkcd comic.
make me a sandwich

Thanks to @hamvocke, @coreyblaser, @AnthonyMastrean, @icommitfelonies, @the_scott, @loweschmidt, @_lpag, and @Jay3141592653 for this tip.

#3 Return to the Previous Directory

If you ever moved to another directory to accomplish a task, and then lamented that you had to type out the path to return to your previous location, this is the tip for you. The cd command can accept the dash character (-) instead of a directory. When it sees this, it will move you to the previous directory you were in.

cd -

Here, I jump over to /tmp and move back to my home directory w/o having to type it out.
cd previous directory

Thanks to @scrptktty, @felis_rex, @malanalysis, @kenglxn, and @axcdnt. Note this behavior is specific to the cd command so you can’t pass the - to any old command and expect it to work. Though there are other programs that adopt this convention as @gunar points out.

#4 Use a Process Where a Command Expects a File

One of Bash’s most powerful features is the ability to pass the output of one command to another using pipes. But what if you need to send the output of two different commands to a single process? Or, you need to pass the output of a process that will accept a file path as an argument but doesn’t read from STDIN?

The Process Substitution feature in Bash lets you run a process and expose its output as a file that can be used by other commands. To use this feature, wrap the command whose output you need in the syntax <(command) and place it where a file argument would normally go. For example, if you wanted to compare the sorted versus unsorted versions of a file you could run:

diff -u some-file <(cat some-file | sort)

Here is another example where I use diff, curl, and the process substitution feature to compare the repsonses to two different requests to the same web page:

compare curl requests

Thanks to @tectonic, @Supermathie, @tacertain, @mwotton for this tip. This one was new to me so I’m excited to get to try it out.

#5 Reference Argument to Your Previous Command

Sometimes you’ll find yourself running a set of commands against the same file again and again. If it’s a particularly long file name you’ll probably get sick of typing it over and over again. Luckily, in Bash you can use !$ to reference the last argument of the previous command. Here I create a new git repo for a new project, and setup the README w/o having to type the path to the file repeatedly.

reference last argument

Thanks to @neilkod, @johngoulah, and @alexbires for this tip. Note that this tip along with the tip to re-run a command with sudo both take advantages of features of the GNU Readline and History libraries. This means they aren’t totally Bash specific as @miah_ pointed out, and you may be able to use these with other tools.

Next Time

There were also so many great tips about working with your bash history, many of them new to me, that I’m going to do a follow up post that focuses just on this topic. I hope you’ll enjoy it.

In the meantime, you might want to checkout Opterview, a service I built which lets you interview SRE and DevOps candidates at the command line, and which I used to generate the GIFs. See you next time!