David Korn - 2001.
A shell script is a series of commands, lists or pipes executed in a shell non-interactively. A shell script does not need to be saved to a file. e.g.
sa101$ if [ -f /etc/shadow ];then echo "got it! Now what?";\ else echo "Search me!";fi
The above is a perfectly valid shell script. Note that no file was required. We can re-execute the script by using the shell history.
NB. the escape character (\) is used to indicate that the line continues without a line feed, you should not include it when typing this script at the terminal.
The shell can keep a record of your previous commands. There are various mechanisms for accessing the shell history. Bash can use both the old csh ! history syntax, emacs or vi. The editor for shell history can be set with the environment variable FC_EDIT.
If you know vi the easy way to access history is to set the shell option vi.
sa101$ set -o vi
Having set the option <esc>k puts you into vi mode with the last command issued, available as if at the bottom of a text file. We can now use the usual vi commands to edit the line or just hit return to re-execute it.
Hitting k will step us back through the history (i.e. go up the history file), j will step us back down the lines of the history file.
sa101$ mkdir test sa101$ cd test sa101$ touch file1 sa101$ set -o vi sa101$ ^[k sa101$ k sa101$ f1r2 sa101$ ls -l
NB: ^[ is the notation for a keyboard <esc> key.
If you are using vi mode you can search the history for a string with the same command that is used in vi and in less. i.e. /<string>.
sa101$ history ... 468 vi train_news 469 sudo cp /var/tmp/t train_news 470 vi train_news 471 sudo bash 472 pwd 474 cd ~ 475 pwd 476 cd clients 477 cd fulford/clients 478 ls 479 cd po* 480 cd sa101 481 make xhtml 482 sudo make xhtml 483 pwd 484 ls 485 d 486 cd /usr/local/web 487 cd cl 488 set -o vi 489 vi train_news 490 vi sa101_news 491 sudo bash 492 history 493 set -o vi 494 make xhtml 495 history sa101$ set -o vi sa101$ ^[K/web sa101$ cd /usr/local/web
After issuing the /<string> command it is possible to step back through each instance of the string <string> by hitting the next key (n).
A list of recent commands issued can be obtained with the history command. This will list the number of previous commands set by the environment variable HISTSIZE. The default value for the bash shell is 500. (This can be changed by users setting a different value in their home directory .profile.)
The commands are numbered and can be re-executed by using the ! character, also known as bang or shriek.
E.g.
sa101$ history 5
492 man history
493 history|wc
494 history|less
495 history -5
496 history 5
sa101$ !493
history|wc
497 1626 492910
This is known as history substitution and can be incorporated into shell scripts.
If you are going to use history frequently, it is useful to use the alias command to reduce the keystrokes required to obtain the history list.
sa101$ alias h=history sa101$ h 5 502 ls -l 1* 503 date 504 grep fulford /etc/passwd 505 df 506 h 5
Read the man page for history.
Read the man page for bash and find the references to HISTSIZE, HISTFILE and HISTFILESIZE.
Set the value of HISTSIZE in $HOME/.profile.
Use the table of commands and metanotation below to revise and consolidate your learning so far.

Copyright
© 2003-2017
Clifford W Fulford.
Fulford Consulting Ltd.
Regd. Co. 4250037 in England & Wales.
Regd. office 162, Edward Rd. Nottingham NG2 5GF, England, UK.
Related web sites: City Linux | Flare Support | West Bridgford | Fulford Portal | Joan Mary Fulford (Nottingham Writer) | Fulford Gallery | Amharic Interpreter | Arabic Interpreter | Tigrinya Interpreter
The layout and associated style sheets for this page are taken from the World Wide Web Consortium and used here under the W3C software licence.