Next Up Previous Contents Index
Command History and Tab Completion

3.3 Command History and Tab Completion

It doesn't take long before the thought of typing the same command over and over becomes unappealing, at best.

Linux users don't feel any differently about that, either. But in Linux, since you can string together commands at the shell prompt, one minor typo in a couple lines of a command could mean that all that typing was in vain.

So there's a solution: It's called command-line history. By scrolling with the up and down arrow keys, we can find plenty of our previously typed commands -- including the ones with typos.

Let's try it by taking a look again at sneakers.txt. The first time, however, at the shell prompt, we'll type:

cat sneakrs.txt

Oops! Nothing happens, of course, because there is no sneakrs.txt file. No problem. We'll just use the up-arrow key to bring back the command, then use the left-arrow key to get to the point where we missed the ``e.'' Insert the letter and press [Enter] again.

Voila! We now see the contents of sneakers.txt.

The bash shell can store up to 1,000 commands.

  • Tip: By typing the env command at a shell prompt, we can see the environment variable that controls the size of the command-line history. The line which reads, "HISTSIZE=1000" tells us that bash will store 1,000 commands in its history.
  • The command-line history is actually kept in a file, called .bash_history in our login directory. We can read it in a number of ways: by using pico, cat, less, more, and others.

    Be prepared, though: the file can be pretty long.

    Let's read it with more:

    more .bashhistory
    

    To move forward a screen, press [Space]; to move back a screen, press [B]; to quit, press [Q].

    Another time-saving tool is known as tab completion. If you type part of a file or pathname then hit the [Tab] key, bash will present you with either the remaining portion of a the file/path, or a beep. If we get a beep, we can press [Tab] again to obtain a list of the files/paths that match what we've typed so far.

    So even if we do turn off the machine at the end of the day, we probably won't have to work too hard in order to remember the command to update locate's database: The chances are good that the command will be stored in the command-line history or can be completed with tab completion (as long as we remember the start of the pathname for the command).

    Both tab completion and command-line history can be useful to help you use a command you've forgotten, as well.

    Let's try tab completion to update locate's database. First, su to root.

    Now, at a shell prompt, type the beginning of the path:

    /etc/cr
    

    Now, press the [Tab] key and tab completion will complete the path to /etc/cron (and you'll hear a beep). Press [Tab] again, and you'll be presented with a list of possible completions:

    cron.daily  cron.hourly  cron.monthly  cron.weekly  crontab
    

    So add the .daily to the command (or simply type the .d and press [Tab] again). Press [Tab] again, and the result will be:

    logrotate   tetex.cron    tmpwatch     updatedb.cron
    

    And there's our updatedb.cron command. We can simply add it to the end of the path we've got so far (or just type u and press [Tab] yet again) and press [Enter] to run updatedb.cron.


    Next Up Previous Contents Index