UNIX Shell History

To see a list of commands you have used you can use the history command. You can also use common editor commands to navigate quickly to specific commands in the history.

If you want to 'record' a set of commands and their outputs use the script command, which writes to a file in the current directory (called typescript by default).

History

The history command is a Korn/Bash shell built-in command that lists previous commands entered. The Korn shell saves commands that you entered to a command history file, usually named $HOME/.sh_history or $HOME/.bash_history. Using this command saves time when you need to repeat a previous command.

By default, the Korn shell saves the text of the last 128 commands for nonroot users and 512 commands for the root user. The history file size (specified by the HISTSIZE environment variable) is not limited, although a very large history file size can cause the Korn shell to start slowly.

Navigation

In bash under Linux you can use the cursor keys to navigate your history. 

In ksh you can navigate using standard vi keystrokes using:

set -o vi

To use cursor keys in ksh you can use this...

set -o emacs

alias __A=$(print '\0020') # ^P = up = previous command

alias __B=$(print '\0016') # ^N = down = next command

alias __C=$(print '\0006') # ^F = right = forward a character

alias __D=$(print '\0002') # ^B = left = back a character

alias __H=$(print '\0001') # ^A = home = beginning of line

Add them to .kshrc or your .profile as necessary.

History Management

To view the history:

history


  991  ps -ef | grep tns  992  lsnrctl stat  993  exit  994  cat /etc/oratab

To run the command numbered 992 in the snippet shown above...

!992

Each line has a position number. To delete an entry from the history based on that position number...

history -d 992

To save any modifications to the history file...

history -w

TODO       fc [-e ename] [-lnr] [first] [last]       fc -s [pat=rep] [cmd]              Fix Command.  In the first form, a range of commands from first to last is selected from the history list.  First and last may be specified as a string (to locate the last command beginning with that string) or  as  a              number  (an  index  into  the  history list, where a negative number is used as an offset from the current command number).  If last is not specified it is set to the current command for listing (so that ``fc -l -10''              prints the last 10 commands) and to first otherwise.  If first is not specified it is set to the previous command for editing and -16 for listing.
              The -n option suppresses the command numbers when listing.  The -r option reverses the order of the commands.  If the -l option is given, the commands are listed on standard output.  Otherwise,  the  editor  given  by              ename is invoked on a file containing those commands.  If ename is not given, the value of the FCEDIT variable is used, and the value of EDITOR if FCEDIT is not set.  If neither variable is set, is used.  When editing              is complete, the edited commands are echoed and executed.
              In the second form, command is re-executed after each instance of pat is replaced by rep.  A useful alias to use with this is ``r="fc -s"'', so that typing ``r cc'' runs the last command beginning with ``cc'' and typ‐              ing ``r'' re-executes the last command.
              If  the  first  form is used, the return value is 0 unless an invalid option is encountered or first or last specify history lines out of range.  If the -e option is supplied, the return value is the value of the last              command executed or failure if an error occurs with the temporary file of commands.  If the second form is used, the return status is that of the command re-executed, unless cmd does not specify a valid history  line,              in which case fc returns failure.

Script

script

Script started, file is typescript

<run your commands here>

exit

Script done, file is typescript