Give me that one command you wish you knew years ago caught my eye yesterday, and I thought I’d tell you one of my favorite little commands in the bash: it’s called fc (fix command), and it is described in the manual, of course.
fc selects commands from the bash history, and by using an alias on it, I can quickly repeat the last command line which begins with a particular string. An example:
I submit a longish command:
$ dig +noall +answer +norec @k.root-servers.net . soa
...
If I want to repeat that later, I use
$ r dig
dig +noall +answer +norec @k.root-servers.net . soa
...
which searches backwards in the shell’s history for the first command which
begins with the string I specify. (r d
would have sufficed, because I haven’t used any other command later which begins with a d
.) The full command line is printed to stdout
and then executed. (This is like what !
does, but I prefer my !
undiluted,
so I disable that by setting histchars=''
.)
r
is an alias I set up like this:
export HISTCONTROL=ignoreboth
alias r='fc -e -'
HISTCONTROL
defines how bash saves history entries: ignoreboth
is a combination of
ignorespace
(lines which start with a space) and ignoredups
(ignore
duplicates). The latter is important for the following situation: Suppose I’m
waiting for a particular output, and I repeat the command in series:
$ r dig
...
$ r dig
...
$ r dig
...
By setting ignoredups
I ensure duplicate commands are not saved in the shell’s history.
Give it a try, and while you’re at it, try the v
while you’re in bash’ editing mode. :-)