About nixCraft

Topics

Repeat last shell command that started with a particular word

Posted by Vivek Gite [Last updated: July 8, 2007]

Bash / CSH shell offers command history feature. Most of you may be aware and using of UP / DOWN arrow keys to recall previous commands. History expansions introduce words from the history list into the input stream, making it easy to repeat commands, insert the arguments to a previous ommand into the current input line, or fix errors in previous commands quickly.

To repeat last command that started with bar word type i.e. refer to the most recent command starting with string bar):
! bar

For example, let us run lighttpd to test config file:
# /usr/sbin/lighttpd -t -f /jail/usr/local/etc/lighttpd/lighttpd.conf
Output:

Syntax OK

Type few commands:
# pwd
# date

Now to repeat last command that started with lighttpd word type
# !lighttpd
Output:

lighttpd -t -f /jail/usr/local/etc/lighttpd/lighttpd.conf
Syntax OK 

Want to refer to the previous command, just enter
!!

Related shell tip

E-mail this to a Friend    Printable Version

You may also be interested in other helpful articles:

Discussion on This Article:

  1. devnet Says:

    I also like to use Control-R to do a search of my history file to find commands that may not have been the last one that I ran.

    Control-R will bring up a search dialogue where I can type in one part of my command…say, a package name or other, and it will find it and select it again. This speeds up remembering that one command you did last week :D

  2. vivek Says:

    devnet,

    Ah, I forgot about magical CTRL+R.

    Appreciate your post!

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tags: , , , , , , , ,

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Powered by Open source software.