LWN.net Logo

CLI Magic: Using command history in the bash shell (Linux.com)

Linux.com presents an excerpt from chapter 9 of the Third Edition of A Practical Guide to Red Hat Linux: Fedora Core and Red Hat Enterprise Linux, which looks at the bash history mechanism. "The Bourne Again Shell's history mechanism, a feature adapted from the C Shell, maintains a list of recently issued command lines, also called events, providing a quick way to reexecute any of the events in the list. This mechanism also enables you to execute variations of previous commands and to reuse arguments from them."
(Log in to post comments)

CLI Magic: Using command history in the bash shell (Linux.com)

Posted Jul 4, 2006 1:37 UTC (Tue) by xoddam (subscriber, #2322) [Link]

I learned a bit about ! history references when I used to use csh in the days before GNU, but upgrading to tcsh gave me cursor keys. I did use !?blah? sporadically on bash for a while until I discovered the incremental search feature: using the same keys as emacs (ctl-r for search backwards and ctl-s for search forwards), you can interactively find substrings in your history.

! and such features are nice to know about for historical reasons (and to prove you're a l33t h4x0r), but they're about as useful as ex. AFAIK there are no terminals left that can't rewrite the prompt line; let me know if anyone's stuck with a Teletype® :-)

CLI Magic: Using command history in the bash shell (Linux.com)

Posted Jul 4, 2006 3:45 UTC (Tue) by mkerrisk (subscriber, #1978) [Link]

! and such features are nice to know about for historical reasons (and to prove you're a l33t h4x0r), but they're about as useful as ex. AFAIK there are no terminals left that can't rewrite the prompt line; let me know if anyone's stuck with a Teletype® :-)

Much of the ! syntax is arcane, but a few things are very useful still. How about:

$ vi myprog.c
...
:wq
$ cc !$

And similar. That's faster than cursor+editing keys.

CLI Magic: Using command history in the bash shell (Linux.com)

Posted Jul 4, 2006 4:24 UTC (Tue) by xoddam (subscriber, #2322) [Link]

Well, you learn something new every day. Thanks!

... but this example hardly disproves that the syntax is arcane! :-)

CLI Magic: Using command history in the bash shell (Linux.com)

Posted Jul 4, 2006 7:38 UTC (Tue) by glettieri (subscriber, #15705) [Link]

!$ is useful. However, I prefer "Alt + .": it rewrites the last argument from the previous command line, and you can use it many times to get arguments further down in the command history.

it just gets better!

Posted Jul 4, 2006 7:46 UTC (Tue) by xoddam (subscriber, #2322) [Link]

"Invent something in the morning and they're making cheap imitations by
noontime!" -- Sesame Street

(I wish I could remember the name of the inventor; Google can't help me)

CLI Magic: Using command history in the bash shell (Linux.com)

Posted Jul 4, 2006 8:46 UTC (Tue) by NAR (subscriber, #1313) [Link]

It might be even faster if you have vim and a Makefile for myprog.c to startup vim and issue the
:make
command in vim to compile the file. There might even be a plugin or something that would compile the source without a makefile.

Bye,NAR

CLI Magic: Using command history in the bash shell (Linux.com)

Posted Jul 4, 2006 12:07 UTC (Tue) by tzafrir (subscriber, #11501) [Link]

:make myprog

And I have been through enough situations where the terminal is not fast enough. Either because it is dog slow or because I know exactly what I want to type.

Another useful combination:

$ which mozilla
/usr/bin/mozilla
$ sh -x `!!`

CLI Magic: Using command history in the bash shell (Linux.com)

Posted Jul 4, 2006 12:36 UTC (Tue) by Dom2 (guest, #458) [Link]

M-. is usually quicker to type than "!$"...

CLI Magic: Using command history in the bash shell (Linux.com)

Posted Jul 4, 2006 7:37 UTC (Tue) by sveinrn (guest, #2827) [Link]

There is one feature I have been missing...

Some years ago I was using MatLab for Windows extensively. There it is possible to type the first few letters of the command and then use arrow-up to get to the previous lines starting with those letters. So when I e.g. has issued a complicated mount command that I need again, and after that 30 other commands, including two other unrelated mount commands, I want to be able to type "mount<up><up><up>" and get back the command I want. Also, something like "*mysource.c<up><up>..." to get all commands containing this text would be nice. I think using the "history" command is too time-consuming, and I usually ends up typing the command again.

Is there anybody that knows how to do something like this?

CLI Magic: Using command history in the bash shell (Linux.com)

Posted Jul 4, 2006 8:27 UTC (Tue) by abovett (subscriber, #13139) [Link]

Not quote the same, but try <ctrl>-R, then type some characters - which will recall the first command that _contains_ those characters. Then hit <ctrl>-R repeatedly until you get to the command you want. Hit Enter to execute it, or Esc to recall it to the current line for further editing.

HTH

CLI Magic: Using command history in the bash shell (Linux.com)

Posted Jul 4, 2006 9:28 UTC (Tue) by gip (subscriber, #20897) [Link]

Quote:
I want to be able to type "mount<up><up><up>" and get back the command I want.

Yes, I had this when I used 4DOS and liked it so I set up bash to do the same thing (it's a "readline" capability, I think).

Create a .inputrc file in your root and put in

"\e[5~": history-search-backward
"\e[6~": history-search-forward

Or whatever your PgUp and PgDn keys output. Next login it should work.
other stuff I use in .inputrc

"\e[2~": kill-whole-line
"\e[3;2~": kill-line

So Ins clears the line and SHIFT-Del clears from the cursor to the end

Hope this helps.

Ciao
G

CLI Magic: Using command history in the bash shell (Linux.com)

Posted Jul 7, 2006 10:39 UTC (Fri) by ekj (guest, #1524) [Link]

For many cases, typing C-r mount and then repeatedly C-r until you find the correct one is equally simple.

CLI Magic: Using command history in the bash shell (Linux.com)

Posted Jul 4, 2006 9:34 UTC (Tue) by etienne_lorrain@yahoo.fr (guest, #38022) [Link]

To type the begining of an old CLI command and search backward/forward another command starting with the same chars with PageUp/PageDown, I am using this file for a long time (I think since RedHat 4.2):

$ cat ~/.inputrc
set meta-flag on
set input-meta on
set convert-meta off
set output-meta on
"\e0d": backward-word
"\e0c": forward-word
"\e[h": beginning-of-line
"\e[f": end-of-line
"\e[1~": beginning-of-line
"\e[4~": end-of-line
#"\e[5~": beginning-of-history
#"\e[6~": end-of-history
"\e[5~": history-search-backward
"\e[6~": history-search-forward
"\e[3~": delete-char
"\e[2~": quoted-insert

Recent behavior changes driving me nuts

Posted Jul 4, 2006 7:54 UTC (Tue) by jzbiciak (✭ supporter ✭, #5246) [Link]

I know there are probably more "correct" ways to do this, but this is a long standing habit. I have a tendency to open a line with a # (thereby making it a comment) and then expanding out something in my history with !.

So, for example:

#!t

might expand out into

#tail -f /var/log/messages

I then up-arrow, remove the # and edit the line to my heart's content if it was the line I'm looking for. This works great w/ older bash, such as 2.05a. Recent bash versions, such as 3.1.17, don't seem to do this.

Does anyone know a way to reenable this in bash?

Recent behavior changes driving me nuts

Posted Jul 4, 2006 8:50 UTC (Tue) by abovett (subscriber, #13139) [Link]

If the histverify option (set with shopt) is set, you don't need to use the # trick. Any command you type using history expansion (with !) will be recalled to the prompt for checking and editing before it is executed - hit Return to execute it. In other words, if my history looks like:

540 cat SQ9285.txt
541 ls Documents/
542 history

and I type:

rm !540:1

and hit Return, then I get:

rm SQ9285.txt

with the cursor positioned at the end of the line.

CLI Magic: Using command history in the bash shell (Linux.com)

Posted Jul 4, 2006 14:39 UTC (Tue) by gjmarter (subscriber, #5777) [Link]

I have a command called showcmd which is implemented as follows:

history | grep "$1" | grep -v showcmd | sort -r -k 2 | uniq -f 1 | sort -n | tail

I believe it only works in bash. This allows me to quickly find the most recent commands matching a pattern without repeats.

Also if the command that I want is a small variation of one of those commands then M-^ allows me to edit the line in the shell as if it had been typed it or arrowed back to.

Copyright © 2006, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds