Bash History Mechanism
Buffer/mechanism that holds previously issued commands
which can be recalled.
Most modern shells offer some form of command recall but features may vary.
When a command line is entered, it is cached in the history buffer.
Lines added to the cache are numbered sequentially as added.
Whole line is added including command delimiters.
Command line delimiters are NOT processed at this time.
History references to previous lines are evaluated immediately.
Important environmental variables for history mechanism
HISTSIZE - number lines to remember in current login.
Once list is full, older lines discarded. (queue)
HISTFILESIZE - number of lines to remember between logins.
$HOME/.bash_history contains these.
Re-written on log-out.
HISTFILE - name of history file. Default for bash is .bash_history.
Adjust these in your .bash_profile in your $HOME.
Using history mechanism to recall command lines.
history - the history command will list the current history buffer.
with line numbers.
The history recall mechanism features :
! - sometimes called the "bang" recalls line stored in history.
! is resolved before any other actions when parsing command line.
Only then is line stored in history list.
! not stored in history.
!! - double bang recalls and runs last line entered.
! - bang. Followed by a valid history line number (#)
Recalls whole line and re-runs it.
:p - :p prints but does not run specified line. e.g :
!!:p
Useful if you want to refresh an old line in history list.
Arguments on command line are numbered from start of line.
Quoted strings, including grave quotes ``, are single items.
Delimiters, ;, &, &&, ||, are numbered individually.
First word on line (usually a command) is 0, only a single 0 per line.
- Accessing elements of command line.
122> touch five && touch sticks
0 1 2 3 4
123> echo won to tree for > sticks
0 1 2 3 4 5 6
124> cat one tree five > stones
0 1 2 3 4 5
Use a colon followed by argument index to further qualify a history recall.
125> echo !123:5
>
126> echo "!122:4 !122:2 124:5"
sticks && stones
Note quotes. Without the quotes, this would have expanded to
echo sticks && stones
The shell would have seen the && as a conditional AND delimiter and
tried to run stones if echo sticks was successful.
- Further targeting
When working with files and their paths, history has an additional
targeting qualifiers.
h - head - lists everything up to last / in filename string.
t - tail - lists everything after last / in filename string.
r - root - lists everything up to last extension in filename string.
e - extension - lists just the last extension.
500> ls -l /etc/mail/exim.conf.old
501> echo !500:2:h
/etc/mail
502> echo !500:2:t
exim.conf.old
503> echo !500:2:r
/etc/mail/exim.conf
504> echo !500:2:e
old
# both head and root can be repeated.
505> echo !500:2:t:r
exim.conf
506> echo $500:2:h:h
/etc
- Using arrow keys to access history.
Newer shells such as bash also allow users to use the arrow keys
to recall previously entered command lines.
[UP] - scroll into past entries.
[DOWN] - scroll forward, only good if not on latest line.
- Editing entered/recalled command lines.
provides editing features of Emacs to edit a command line.
It can also be set to use the vi editor.
For vi
set -o vi
Enter commands and arguments on the command line. Don't hit enter.
Now hit [esc].
You can now use the vi cursor command mode keys to navigate
or alter the command line.
h - move left on the line.
l - move right on the line.
k - recall a previous command line from history list.
j - move forward in the history list.
cw - change word under the cursor to a new word.
You can even do substations.
500 echo "this is a long echo line"
this is a long echo line
!500:s/this/That/
That is a long echo line
If you use PuTTY, you can also highlight any string on the screen, and
right click on the command line and PuTTY will paste what was highlighted.