Bash tips

If you work with Linux there is a pretty good chance you spend at least some time working with Bash. There are of course other available shells, however I personally use bash day to day. Mainly because if you jump onto a new system bash will likely be the default shell.

This post goes through some useful tips and keyboard shortcuts which will hopefully save you time. If your new to bash I would recommend learning a couple of shortcuts at a time, if you've been using bash for years you probably know most of this already.

Note: Some of the shortcuts below assume you are using the default emacs style line editing mode. You can check this by running set -o | grep emacs. For more info have a look at the Bash command line editing docs.

Tab completion

When using bash I spend half of my time hitting tab. When you hit tab bash will try to complete the path your currently typing based on what you've entered so far. For example say you want to run the following:

cd /etc/sysconfig/network-scripts/

You can do this by typing:

cd /e<tab>/sysco<tab>/net<tab>

OK, so you probably knew that, but did you know bash can also be configured to give command specific completion? For example say you want to check the status of a service but can't quite remember the service name:

systemctl status au

Then hit tab and bash will complete the command by replacing au with auditd.service.

systemctl status auditd.service

If you haven't done so already I would also recommend installing the bash-completion package which provides additional completion functions for bash.

Exit quickly

To close bash you can type logout or exit, however you can also send an end-of file character:

ctrl + d

Note: if you find the behaviour annoying you can disabled it with set -o ignoreeof.

Moving the cursor

The following shortcuts let you move the cursor quickly:

  • ctrl + a : move to the start of the line

  • ctrl + e : move to the end of the line

  • ctrl + f : move one letter forwards

  • ctrl + b : move one letter backwards

  • alt + f : move one word forwards

  • alt + b : move one word backwards

Recalling history

Retyping long commands can be a pain. You can get around this by pressing:

ctrl + r

This will bring up a reverse search prompt that looks something like this:

(reverse-i-search)`':

You can then start typing part of a command you've previously used:

(reverse-i-search)`fork': grep fork master.log | sed -r 's/(Jun.*[0-9]{2}):[0-9]{2}:[0-9]{2}.*/\1/' | uniq -c

You then have a few options:

  • ctrl + r : Keep cycling through matches.

  • Enter : Run the command.

  • Esc : Edit the command before running it.

  • ctrl + c : Get back to a prompt without running anything.

Repeat the last command

While running as a standard user it's easy to forget to use sudo and get a message like this:

$ systemctl restart httpd.service
Failed to issue method call: Access denied

Instead of typing sudo followed by retyping the whole command again, you can just type:

sudo !!

Bash will then replace !! with the previous command and run it:

$ sudo !!
sudo systemctl restart httpd.service