A bioinformatician’s best tool is his shell. While some have already mastered the dark arts of the bash shell, I often see beginners (and even catch myself at times!) unknowingly repeating key sequences when they could be getting the same result with a few simple built-in keybindings or programmatic shortcuts. Let’s have a look at some of the most useful bash shortcuts that no self-respecting bioinformatician should be without. This is by no means an exhaustive list of what Bash has to offer but will hopefully serve to save a few key presses 🙂

Command Editing

The Bash shell has its own set of cursor movement keybindings. Depending on your terminal emulator and desktop environment settings, you may get away with such bindings as Ctrl-Left and Ctrl-Right to move from word to word and a tap on the Home and End key may bring you to where you’re headed. Unfortunately, these are not standard on all terminal emulators. The following, however, are.

Ctrl-a will bring you back to the beginning of the line while Ctrl-e will bring you to the end.

Alt-f will move the cursor forward one word while Alt-b will do the opposite.

Alt-Backspace will delete the last word.

Ctrl-u will delete everything from the cursor to the beginning of the line, Ctrl-k will delete everything from the cursor up to the end of the line and Ctrl-y will paste the last deleted text.

cutpaste

Ctrl-e Ctrl-x will open your command line editor (as specified in the $EDITOR environment variable) with the current command buffer, allow you to edit the command to your liking and execute it on exit.

Finally, Ctrl-_ will undo the last change.

Command Recall

No one should be subject to endless up arrow presses in an attempt to find that perfectly crafted command. Much more efficient is Ctrl-r, arguably one of the most underrated features of Bash, which will execute an interactive backwards search on previous commands.

ctrlr

In fact, bash has an whole slew of commands dedicated solely to the command history, many of them starting with an exclamation mark. For example, just as the $_ environment variable stores the last command, !! is your handy shortcut to executing it. To fetch all the arguments of the previous command, !:* will do the trick.

lastargs

Typo? Re-run the last command with a correction with ^exho^echo (here replacing the erroneous “exho” with “echo”).

replace

Another neat feature is the Alt-. (or ESC-.) keybinding which will add on the last word of the last command to the shell (especially useful with paths).

Jobs

This is an easy one! Ctrl-z suspends the present job, jobs will show the stopped jobs while fg will bring the specified job back in the foreground (bg in the background as if invoked with &). I find this particularly useful when switching back and forth between an editor and the terminal.

In the case of a long running command that you wish to keep running when closing the session, a quick disown of a background process will keep it alive and well even when thrown a SIGHUP on exit (prefixing the command with nohup on start and appending & has the same effect).

jobs

Braces

Brace expansion can help with menial tasks such as moving a file (ex : mv somefile{.txt,.bak}) or removing a specific series of similarly named files (ex : rm somefile{1,2,3,5,8}.txt).

braces

Directory Navigation

The environment variable \$OLDPWD is automatically set to the last directory (and \$PWD to the current). Pass – as argument to cd to return to the last directory (cd –).

cdmin

Cleanup

Sick of what you’re seeing on the screen? Ctrl-l will clear it while reset will restart your shell.

Diagnosis

Finally, a fun little one liner to help diagnose sore points in your bash usage (usage counts of commands in your Bash history).

history | awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl | head -n10