teh bigbro blog(tm)
Bigbro's foray into the scary world of blogging

Fri, 19 May 2006

Bang Commands

Thanks to Kevin Lyda for posting some useful examples of bash (and other shell) bang expressions to the ILUG mailing list. Reproduced here so that I'll remember them :-)

Most modern shells support csh's bang commands - bash, tcsh and zsh all do. While some bang commands don't work in all those shells, all of the following do. The plus side to using them is that no matter what shell you're using, these shortcuts will work.
For the purposes of these tips, every tip will 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.c
Accessing 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.c
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.
posted at: 17:13 | path: /technical | permanent link to this entry


copyright © 2005-2008, Gareth Eason