fortypoundhead.com

HISTORY

Posted On 2007-05-01 by FortyPoundHead
Keywords: Command Reference
Tags: Linux Commandline Linux
Views: 1543


Command Line history

SYNTAX
history
history [n]
history -c
history -d offset
history [-anrw] [filename]
history -ps arg

KEY
-c Clear the history list. This may be combined with
the other options to replace the history list completely.

-d offset
Delete the history entry at position offset.
offset should be specified as it appears when the history is displayed.

-a Append the new history lines (history lines entered since
the beginning of the current Bash session) to the history file.

-n Append the history lines not already read from the history file
to the current history list. These are lines appended to the
history file since the beginning of the current Bash session.

-r Read the current history file and append its contents to the history list.

-w Write out the current history to the history file.

-p Perform history substitution on the args and display the result
on the standard output, without storing the results in the history list.

-s The args are added to the end of the history list as a single entry.
With no options, display the history list with line numbers. Lines prefixed with with a `*'' have been modified. An argument of n lists only the last n lines.
When any of the `-w'', `-r'', `-a'', or `-n'' options are used, if filename is given, then it is used as the history file. If not, then the value of the HISTFILE variable is used.

Recalling a previous command

Pressing the UP arrow will return to previous commands.

To return to a previously entered command, type ctrl-r and then begin typing the command. This will finish the command for you as you type. If you can remember to use ctrl-r, it will become invaluable for repeating longer commands.

To find a specific command among many previous commands; pipe history through grep:
history|grep -i first few letters of command

History Expansion

History expansions introduce words from the history list into the input stream, making it easy to repeat commands, insert the arguments to a previous command into the current input line, or fix errors in previous commands quickly.

History expansion takes place in two parts. The first is to determine which line from the history list should be used during substitution. The second is to select portions of that line for inclusion into the current one.

The line selected from the history is called the event, and the portions of that line that are acted upon are called words. Various modifiers are available to manipulate the selected words. The line is broken into words (several words surrounded by quotes are considered one word).

History expansions are introduced by the appearance of the history expansion character, which is `!'' by default. Only `\'' and `'''' may be used to escape the history expansion character.

Several shell options settable with the shopt builtin may be used to tailor the behavior of history expansion.

The `-p'' option to the history builtin command may be used to see what a history expansion will do before using it.

The `-s'' option to the history builtin may be used to add commands to the end of the history list without actually executing them, so that they are available for subsequent recall.

Event Designators
An event designator is a reference to a command line entry in the history list.


! Start a history substitution, except when followed by a space,
tab, the end of the line, `='' or `(''.

!n Refer to command line n.

!-n Refer to the command n lines back.

!! Refer to the previous command. This is a synonym for `!-1''.

!string Refer to the most recent command starting with string.

!?string[?] Refer to the most recent command containing string.
The trailing `?'' may be omitted if the string is followed
immediately by a newline.

^string1^string2^ Quick Substitution. Repeat the last command, replacing string1
with string2. Equivalent to !!:s/string1/string2/.

!# The entire command line typed so far. Word Designators

Word designators are used to select desired words from the event. A `:'' separates the event specification from the word designator. It may be omitted if the word designator begins with a `^'', `$'', `*'', `-'', or `%''. Words are numbered from the beginning of the line, with the first word being denoted by 0 (zero). Words are inserted into the current line separated by single spaces.

For example,

!! designates the preceding command. When you type this, the
preceding command is repeated in toto.

!!:$ designates the last argument of the preceding command.
This may be shortened to !$.

!fi:2 designates the second argument of the most recent command
starting with the letters fi.

Here are the word designators:

0 (zero) The 0th word. For many applications, this is the command word.

n The nth word.

^ The first argument; that is, word 1.

$ The last argument.

% The word matched by the most recent `?string?'' search.

x-y A range of words; `-y'' abbreviates `0-y''.

* All of the words, except the 0th. This is a synonym for `1-$''.
It is not an error to use `*'' if there is just one word in the event;
the empty string is returned in that case.

x* Abbreviates `x-$''

x- Abbreviates `x-$'' like `x*'', but omits the last word.

If a word designator is supplied without an event specification,
the previous command is used as the event.Modifiers

After the optional word designator, you can add a sequence of one or more of the following modifiers, each preceded by a `:''.

h Remove a trailing pathname component, leaving only the head.

t Remove all leading pathname components, leaving the tail.

r Remove a trailing suffix of the form `.suffix'', leaving the basename.

e Remove all but the trailing suffix.

p Print the new command but do not execute it.

q Quote the substituted words, escaping further substitutions.

x Quote the substituted words as with `q'', but break into words at
spaces, tabs, and newlines.

s/old/new/
Substitute new for the first occurrence of old in the event line.
Any delimiter may be used in place of `/''. The delimiter may be
quoted in old and new with a single backslash.
If `&'' appears in new, it is replaced by old.
A single backslash will quote the `&''.
The final delimiter is optional if it is the last character on the input line.

& Repeat the previous substitution.

g Cause changes to be applied over the entire event line.
Used in conjunction with `s'', as in gs/old/new/, or with `&''.Examples (bang commands)

The following bang commands work in not just bash but also tcsh and zsh too.
Not every bang command will work in every shell, but these are pretty universal .

assume these are the last three commands you ran:

% which firefox
% make
% ./foo -f foo.conf
% vi foo.c bar.c

Getting stuff from the last command:

Full line: % !! becomes: % vi foo.c bar.c
Last arg : % svn ci !$ becomes: % svn ci bar.c
All args : % svn ci !* becomes: % svn ci foo.c bar.c
First arg: % svn ci !!:1 becomes: % svn ci foo.cAccessing commandlines by pattern:

Full line: % !./f becomes: % ./foo -f foo.conf
Full line: % vi `!whi` becomes: % vi `which firefox`
Last arg : % vi !./f:$ becomes: % vi foo.conf
All args : % ./bar !./f:* becomes: % ./bar -f foo.conf
First arg: % svn ci !vi:1 becomes: % svn ci foo.cExample from Kevin Lyda (Irish Linux Users'' Group)
Various shells have options that can affect this. Be careful with shells that let you share history among instances. Some shells also
allow bang commands to be expanded with tabs or expanded and reloaded on the command line for further editing when you press return.


About the Author

FortyPoundHead has posted a total of 1974 articles.

 


Comments On This Post

No comments on this post yet!


Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.


Your IP address is:54.80.58.75

Before you can post, you need to prove you are human. If you log in, this test goes away.



Recent Forum Posts

List of Shady Characters
dwirch posted on April 25, 2017 at about 16:39 in Webmaster Stuff

Job Spammer: Bilal Uddin
dwirch posted on April 25, 2017 at about 11:00 in Spammers

Bug Fix: Contact Form Error
dwirch posted on April 21, 2017 at about 11:38 in Site News

Bug Fix: Ophion Time Tracker
dwirch posted on April 9, 2017 at about 11:30 in Site News

Job Spammer: Yogesh Kapadne
dwirch posted on March 31, 2017 at about 8:04 in Spammers

Job Spammer: Sathya Narayana
dwirch posted on March 15, 2017 at about 7:18 in Spammers