# Five things you didn't know Bash could do on Linux

## Bash offers a lot of tools to Linux admins, and it may take you years to get a handle on all the useful ones. Learn five lesser-known Bash scripts for your Linux systems including extended globbing, cdspell shell options, improved Bash history, interactive Bash history search, and unaliasing.

Not long after I first started using Linux I wanted to learn a but more about the bash shell and I typed:

$man bash I quickly discovered that the bash man page was an epic. Page after page of commands, functions, tweaks, tips and more environmental variables than you could seemingly ever use. In the twenty years since, I still discover new tools and functions in Bash that I had no idea were there and that tempt me to write Bash one liners rather than "real" code. In this tip I'm going to explore five of the lesser known (but still useful!) features in Bash that you may not know about. 1. 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: $ shopt -s extblob

Note: You can also run shopt on its own to see what other options, features and secrets are it offers.

Once it's on we can ls a directory and return all files except those suffixed by .tmp by using the simple syntax:

$ls !(*.tmp) Or you can match multiple patterns: $ 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

2. 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 command.

$shopt -s cdspell Now type a simple change directory mistake, for example: $ cd /ect
/etc

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

$pwd /etc The cdspell option will correct transposed characters, missing characters and drop any extra characters. 3. 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: $ !110

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

$!wget Would run the last wget command executed. Less well known are the variations on this: $ !:0

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

$wget http://www.google.com$ !:0
$wget To get the arguments rather than the command you would use: $ 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:

$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: $ export HISTIGNORE

4. Interactive history search
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:

(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.

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

$alias rm='rm -r'$ alias mkdir='mkdir -p'

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:

$unalias rm /tmp/file You can also use the \ symbol to achieve the same result: $ \rm /tmp/file`

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

ABOUT THE AUTHOR: James Turnbull works for the National Australia Bank as the manager of the CERT (Computer Emergency Response Team). He is an experienced infrastructure architect with a background in Linux/Unix, AS/400, Windows, and storage systems. He has been involved in security consulting, infrastructure security design, SLA and service definition and has an abiding interest in security metrics and measurement. James is also involved in the Free and Open Source Software community as a developer and contributor. He has authored several articles, and books including Pro Linux System Administration and Pulling Strings with Puppet: Configuration Management Made Easy.

This was first published in April 2010

Oldest

## SearchDataCenter

• ### Make Linux troubleshooting easier

Sysadmins need to ensure their Linux servers perform properly. Check out these tips for choosing the right tool for the job, ...

• ### The bloom is off the DevOps rose

There's a lot of hype about continuous deployment in DevOps circles, but it can be disastrous if the right culture and ...

• ### On-server GPU additions help with big data, VDI

GPUs offload the burden of demanding computational tasks from central processors. Integrate GPU into enterprise servers to manage...

## SearchServerVirtualization

• ### Don't let antivirus software affect hypervisor performance

Antivirus software has good intentions of protecting your environment, but sometimes can cause a hiccup in hypervisor performance.

• ### Solve multicast snooping problems in a bridged environment

Disabling multicast snooping is sometimes the only way to make sure cluster traffic is handled correctly in a KVM environment.

• ### Streamline management and reduce overhead with Microsoft AVMA

Each virtual machine running on a Windows guest operating system needs its own license. But Automatic Virtual Machine Activation ...

## SearchCloudComputing

• ### Where does the enterprise stand with open source cloud computing?

Created as a free option to compete with cloud providers, open source cloud computing tempts businesses with an array of options ...

• ### Public cloud's reputation fends off cloud hacker attacks

Cloud experts are fighting to protect cloud's good name following the recent iCloud hack, but public opinion is not easily swayed...

• ### Rackspace goes all in with managed cloud

Rackspace rebuffed its suitors and opted to stay independent. What that means for the long-term stability of a company pushing ...

## SearchEnterpriseDesktop

• ### Top five differences between Office 365 and Office 2013

Software as a service may be one reason for choosing Office 365 over Office 2013, but licensing and update frequency are other ...

• ### Windows kiosk mode locks down PCs, but note workarounds

Switching from the Windows Explorer shell to a particular app can be useful for single-purpose devices, but users can still foil ...

• ### Windows patch management tools helpful but have limits

Despite the plethora of software updater products, desktop security is too important to leave to chance. Let's compare Windows ...

Close