Old skool Bash tips

Here are some old skool Bash tips and tricks that I think I wrote for someone about ten years ago.

Extended globbing…

The first function we’re going to look at is extended glob matching. This nifty option allows you to do more sophisicated glob matching than provided in standard Bash, for example match all files except those with a suffix of .tmp. Extended globbing is enabled via the shopt, or shell options built-in:

1
$ shopt -s extglob
Tip
You can also run shopt on its own to see what other options, features and secrets are it offers. Another useful globbing option is globstar.

Once the extglob option is on we can ls a directory and return all files except those suffixed by .tmp by using the simple syntax:

1
$ ls !(*.tmp)

Or you can match multiple patterns:

1
$ ls !(*.tmp|*.log|*.bak)

You can use a small selection of other extglob syntaxes including:

  • ?(pattern-list) Matches zero or one occurrence of the given patterns
  • *(pattern-list) Matches zero or more occurrences of the given patterns
  • +(pattern-list) Matches one or more occurrences of the given patterns
  • @(pattern-list) Matches exactly one of the given patterns
  • !(pattern-list) Matches anything except one of the given patterns

The cdspell shell option

Another useful and little known shell option is cdspell. The cdspell option will correct minor spelling mistakes in your cd commands. You can enable cdspell using the shopt built-in.

1
$ shopt -s cdspell

Now type a simple change directory mistake, for example:

1
2
$ cd /ect
/etc

And presto your little mistake is corrected and Bash has changed you to the right directory.

1
2
$ pwd
/etc

The cdspell option will correct transposed characters, missing characters and drop any extra characters.

The autocd shell option

Related to cdspell is autocd. The autocd option allows you to type the name of a directory (in the current directory) and automatically change into it.You can enable autocd using the shopt built-in.

1
$ shopt -s autocd

Now change into the root directory and type the name of a directory you want to change into.

1
2
3
4
5
$ cd /
$ etc
cd -- etc
$ pwd
/etc

And Bash has changed you to the /etc directory.

Better Bash history

One of the most useful Bash functions is the ability to retain a history of the commands you’ve used. You can use the history command to return a list previously executed commands. You can then use the exclamation mark or bang, !, to retrieve and run previous commands in the Bash shell. For example, you can specify a command from your history by number:

1
$ !110

Would run the 110th command in your history. Or you can specify the command by name:

1
$ !wget

Would run the last wget command executed.

Less well known are the variations on this:

1
$ !:0

Which returns the command portion of the previous command executed, for example:

1
2
3
$ wget https://www.google.com
$ !:0
$ wget

To get the arguments rather than the command you would use:

1
2
3
$ mkdir /tmp/newdirectory
$ cd !*
cd /tmp/newdirectory

Your history, however, can become cluttered with repeated commands and commands you may wish to retrieve such as ls or ps. Bash has an environment variable called $HISTIGNORE to only retain the history you want, rather than every command. Let’s look at my $HISTIGNORE setting:

1
$ HISTIGNORE="&:history:ls:ls * ps:ps -A:[bf]g:exit"

This configuration will prevent repeated commands (the & symbol), and the history, ls, ls *, ps and ps -A binaries and the bg, fg and exit built-in commands from being logged to your command history. Another useful trick is to add [ \t]* which prevents any command starting with a space from being logged in your command history. Finally, we just need to export the variable:

1
$ export HISTIGNORE

Did you also know that, in addition to trimming your Bash history, you can also interactively search from your Bash history? You can type Ctrl-r to enable the search function (this may be familiar to some Emacs users and like Emacs you can also use Ctrl-a and Ctrl-e, go to the start and end of a command respectively amongst other short-cuts). This will launch the following prompt:

1
(reverse-i-search)`':

Then type a command or portion of a command to search through your Bash history and display matching commands. If you find the command you wish you can then run it by hitting Enter or return the matched result to the command line to edit it by hitting Esc.

Unaliasing

Most people know about the alias command that allows you to create modified versions of commands, for example adding the -r option to the rm command or the -p option to the mkdir command:

1
2
$ alias rm='rm -r'
$ alias mkdir='mkdir -p'
Tip
You would generally save your aliases in your Bash configuration.

You can also run the alias command without flags to get a list of the currently enabled aliases.

But you may not know about the unalias command. This allows you to run the command without the alias:

1
$ unalias rm /tmp/file

You can also use the \ symbol to achieve the same result:

1
$ \rm /tmp/file

This will run the rm command without the -r option that would have been added by the alias.