Question: Share your bash tips?

Posted by Steve on Fri 29 Apr 2005 at 02:27

GNU Bash is one of the most common shells in the Linux world, it's also the default shell on Debian systems. People who use it frequently and browse the manual usually learn something new, here I'm going to share two tips I cannot live without. What are yours?

Autocompletion

autocompletion is literally what it's name might suggest, you start to type something and it's completed for you.

This is usually enabled for simple things such as file and directory names, and by default it will be activated by the "TAB" key.

For example if you wish to look at the system's password file you might wish to run:

less /etc/passwd

To save keystrokes you can actually type:

lesTAB /eTAB/passTAB

As you proceed to type the words pressing TAB will automatically complete things for you.

In the Debian bash package there is a file installed called /etc/bash_completion, this adds a lot more useful behaviours to bash including:

To cause your shell to use it run the following command, then login again:

echo '. /etc/bash_completion' >> ~/.bashrc

This will now give you a lot more completions, most usefully I find the following :

apt-get upgTAB

This becomes the familiar "apt-get upgrade", other apt-get, and dpkg commands suddenly understand completion too, so instead of typing "dpkg --search" you can cut this down to "dpkg --seaTAB".

To be honest I don't know the full extent of the completion offered as some of the code in the /etc/bash_completion file is pretty hard to follow, but I know it saves me time.

Why not have a look yourself?

Switching to the previous directory

Assuming that you work in the shell for moving around, and working on files then it's often common to switch directories a lot.

One thing that I often find I want to do is move to a long directory path, do something, then go elsewhere. Frequently I wish to go back to the long directory and do something else i've forgotten.

Even with directory completion it's often a pain to have to retype out the previous directory.

Consider the following example:

cd /home/www/www.debian-administration.org/htdocs/
# do something

cd /home/www/www.steve.org.uk/htdocs
# do something else

cd /home/www/www.debian-administration.org/htdocs/
# Ooops forgot something.

Here we want to move back to a directory we just left, and rather than typing out the full path we can take advantage of the fact that bash remembers our previous directory and sets up an alias for it: -.

To change to the previous directory just run:

cd -

Here's an example showing it in use:

# Change to a long directory.
steve@skx:~$ cd /home/www/www.debian-administration.org/htdocs
steve@skx:/home/www/www.debian-administration.org/htdocs$ 

#
# Do something ..
#

# Go to /tmp
steve@skx:/home/www/www.debian-administration.org/htdocs$ cd /tmp

#
# Realise we wanna go back
#
steve@skx:/tmp$ cd -
/home/www/www.debian-administration.org/htdocs

Other solutions to this problem exist, including pushd, and popd, but I admit I find the simplicity of the "cd -" command much more useful.

Argument Reuse

If you're used to running commands from the shell a lot one thing that's incredibly useful is the ability to reuse argumetns from previous commands.

Say you wished to run the following commands:

cp /etc/passwd my-password-copy
emacs my-password-copy

Here we copy a file somewhere, then attempt to edit it.

Instead of typing out the filename we can take advantage of another of bash's shortcuts - it remembers the last argument to the previous command, and allows you to insert it into the current shell with "Esc .".

Run the following to see how it works:

cat /etc/passwd
cp ESC. .

(That is press Esc, then press '.' afterwards' - the last argument to the previous command is inserted into the command line).

Your tips

What features of the bash shell do you find the most useful? Do you have any interesting tips to share?


This article can be found online at the Debian Administration website at the following bookmarkable URL:

This article is copyright 2005 Steve - please ask for permission to republish or translate.