Helpful bash shortcuts
Unfortunately, Mario and Koopa Troopas have nothing to do with this post. Other than it was the first image hit when I Google Image Searched for, “Shell Tricks”.

I’ve been using Linux for nearly a decade. Typically, that means an Ubuntu server with a Bash shell. During this time, more than a handful of Bash shortcuts have presented themselves to me. Therefore, I thought it prudent to keep a running list, as much for my own benefit as that of anyone else.

What is a shell?

Most Unix systems provide the same basic utilities for working at the shell prompt. The syntax used for writing shell commands is standardized. By this, I mean re-directions, pipes, background processes, variables assignments, quoting, and so on. The standard is called POSIX, which I believe was first standardized 32 years ago by the IEEE. The current edition is here: http://pubs.opengroup.org/onlinepubs/9699919799/ (see the “Shell & Utilities” section, there doesn’t seem to be a great way to link directly to it).

On most Unix systems, the POSIX standard is at least observed, but may oftentimes be extended. In particular, the shell itself is often extended to give a more convenient interactive experience, or to be able to provide more advanced shell programming facilities.

Just as you might find many different shells on the beach, you’ll find many different shells to work in. After all, they are “just” applications. bash is the default on Debian / Ubuntu, (and I also believe MacOS), which makes it the hands down winner in my book. Curious what shell code looks like? Here’s the bash git repository page: https://git.savannah.gnu.org/cgit/bash.git

The shortcuts below are quite helpful in my day to day, but I can’t confirm that they work on other shells (such as, zsh or ksh). In addition, it is doubtful that this list is complete. However, I will do my best to keep it up to date with my discoveries.

Cursor Control Shortcuts in Bash

  • CTRL+A or Home – Move the cursor to the start of the line
  • CTRL+E or End – Move to the end of the line
  • CTRL+B or Left Arrow – Move back one character
  • CTRL+F or Right Arrow – Move forward one character
  • CTRL+Left Arrow or ALT+B or Esc and then B – Move back one word at a time
  • CTRL+Right Arrow or ALT+C or Esc and then F – Move forward one word at a time

Screen Controls Shortcuts in Bash

  • CTRL+L or type clear – Clean up the screen
  • CTRL+S – Pause the screen. Useful if the screen is scrolling long output
  • CTRL+Q – Resume the screen after a pause (CTRL+S)

Search Bash History

  • CTRL+P or Up Arrow – Recall previous command (cycle through CLI history)
  • CTRL+N or Down Arrow – Move to a more recent command (reverse cycle through CLI history)
  • CTRL+R – Reverse search. After starting a reverse search, type the characters unique to your command
  • CTRL+S – Forward search through bash history
  • CTRL+G – Quit the reverse or forward search

Delete Text in Bash

  • Backspace – Delete text behind the cursor
  • CTRL+X then Backspace – Delete ALL text behind the cursor
  • CTRL+K – Remove all text from the cursor to the END of the line

Managing Linux Processes

  • CTRL+Z – Suspend the current process that is running in the foreground. This key combination produces a SIGTSTP signal to the process.
  • Type fg – Brings a process back to the foreground (after suspending it with CTRL+Z)
  • CTRL+C – Interrupt the current process.
  • CTRL+\ – Produces a SIGQUIT signal, and causes the current process to terminate and core dump. This is especially useful when CTRL+C is unresponsive.

Bang Commands

  • !! – Execute last command
  • !pyth – Execute the most recent command that starts with pyth
  • !pyth:p – Display the command that !top would run, and places it at the top of the history
  • !$ – Execute the last word of the last command. For example, if you typed, vim slippySalamander.py then issuing python3 !$ will attempt to execute slippySalamander.py with Python.
  • !$:p – Display the word that !$ would execute
  • !* – Display the last word of the previous command
  • !:*p – Display the last word that !* would substitute

Getting Bash help

To find any kind of general help with Bash issue man bash

Links & References