LINUX TIPS AND TRICKS --- February 23, 2001

Published by ITworld.com -- changing the way you view IT
http://www.itworld.com/newsletters
________________________________________________________________________________

Event History
By Danny Kalev

This week, we use the BASH history utility to retrieve previously 
executed shell commands.

BASH automatically stores a limited number of previously typed 
commands, the default limit being 500 commands. Assigning a different 
value to the variable HISTSIZE changes the default limit. Each command, 
technically referred to as an "event", associates with a number 
according to their sequence (i.e., the most recent event has the 
highest number). The "history" command lists events and their 
associated numbers:

    $ history
    1 ls
    2 mv payroll febpayroll
    3 vi myprog.c

In this example, the user executed three commands before 
typing "history". BASH enables you to traverse previously typed 
commands by pressing the UP-ARROW key (to move up to the previous event 
in the history list) and DOWN-ARROW (to move down to the next event in 
the history list). In most cases, sequential access is sufficient; 
however, repeatedly pressing UP-ARROW can be tedious when scrolling 
through a lengthy list of commands. Instead, you can use the ! operator 
followed by the command's associated number to retrieve a specific 
command.

    $ history
    1 ls
    2 mv payroll febpayroll
    3 vi myprog.c

    $ !1
    ls

In the above example, typing "!1" retrieves event number 1. You can 
also use an initial substring that appears in the command you wish to 
retrieve. For example:
 
    $ !mv
    mv payroll febpayroll

In this case, following ! with the substring "mv" retrieves the 
command "mv payroll febpayroll", which begins with "mv".

You may also use a negative number after the ! operator to reference an 
event using an offset from the list's end. In other words, you can 
instruct the history utility to access n commands from the most recent 
one. For example:

    $ !-2
    mv payroll febpayroll

Let's examine how the "!-2" is processed. The following table contains 
the list of events and their negative offsets:

    event #  offset
    1       typing !-3 puts us here
    2       typing !-2 puts us here
    3       typing !-1 puts us here

As you can see, typing "!-2" retrieves event number 2, which is "mv 
payroll febpayroll".

If you type only ! without a number or an initial substring, the last 
command is assumed:

    $!
    vi myprog.c


About the author(s)
----------------
Danny Kalev is a system analyst and software engineer with more than 10 
years of experience, specializing in C++ and object-oriented analysis 
and design on various platforms including VMS, DOS, Windows, Unix, and 
Linux. His technical interests involve code optimization, networking, 
and distributed computing. He is also a member of the ANSI C++ 
standardization committee and the author of ANSI/ISO C++ Professional 
Programmer's Handbook (Que, 1999). Contact him at linuxnl@excite.com. 
________________________________________________________________________________

ADDITIONAL RESOURCES

Using history and command line editing 
Save keystrokes with a few simple commands 
http://www.unixinsider.com/jsw/lintps_nl/swol-01-1998/swol-01-unix101.html


The language of shells 
Making sense of shell commands 
http://www.unixinsider.com/jsw/lintps_nl/swol-07-2000/swol-0728-unix101.html

Tips on good shell programming practices 
What #! really does

http://www.unixinsider.com/jsw/lintps_nl/swol-09-1999/swol-09-unix101.html

Command line psychology 101 
How the shell program interprets commands

http://www.unixinsider.com/jsw/lintps_nl/swol-02-1998/swol-02-unix101.html
________________________________________________________________________________

COMMUNITY DISCUSSIONS

Linux Software Development
Hone your Linux development skills, share your expertise, and put out 
the occasional call for help in this discussion for programmers of all 
levels. Moderated by Danny Kalev.

http://www.itworld.com/jump/lintps_nl/forums.itworld.com/
webx?14@@.ee6b652/261!skip=197
________________________________________________________________________________

CONTACTS

* For editorial comments, write Andrew Santosusso, Associate Editor, 
  Newsletters at: andrew_santosusso@itworld.com

* For advertising information, write Dan Chupka, Account Executive at:
  dan_chupka@itworld.com

* For recruitment advertising information, write Jamie Swartz, Eastern
  Regional Sales Manager at: jamie_swartz@itworld.com or Paul Duthie,
  Western Regional Sales Manager at: paul_duthie@itworld.com

* For all other inquiries, write Jodie Naze, Product Manager, 
  Newsletters at: jodie_naze@itworld.com
________________________________________________________________________________

PRIVACY POLICY
http://www2.itworld.com/CDA/ITW_Privacy_Policy

Copyright 2001 ITworld.com, Inc., All Rights Reserved.

http://www.itworld.com
