Linux Bang Commands

If you spend a lot of time on the linux command line you quickly find that it requires a lot of typing and retyping commands. I used to find myself using the exact same lengthy command multiple times a day and to get there I would type “history | grep some_command” and then execute it from there. Since I knew enough to get the job done I hadn’t really tried to find more efficient ways of doing the same old thing. But when I found out about the Linux bang (!) commands I realized how wasteful what I was doing really was.

The exclamation mark, in this case, is referred to as a ‘bang’.

  • !!
    This bang command, when entered into the bash shell will run the previous command. It basically does the same thing as hitting the up arrow to take you to the previous command and then hitting enter.
     
  • !ls
    This will run the last command that started with ‘ls’. If you ran ‘ls -al /etc/init.d’ a few commands ago and then you type ‘!ls’ the full command will be run again, assuming that you haven’t used that command since then.
     
  • !ls:p
    This will display the command instead of running it.
     
  • !$
    This one will run the last word of the previous command. This one is mainly useful for substitutions.
     
  • !$:p
    Instead of running the last word of the previous command this will print it out.
     
  • !*
    This bang command will run the previous command without the first word. This one is also only really useful for substitutions as we see in the examples that follow.
     
  • !*:p
    This will print the previous command without the first word.
     

Here are a few examples of how to use these bash bang commands in everyday command line usage :

For the purposes of these examples, every example 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

I found those examples here.

Share

12 Responses to “Linux Bang Commands”

  • Anonymous Says:

    !$ is not meant to run the last word, it is meant as substitution.

    ls -l foo.txt
    vi !$

    in the second command !$ will be substituted with foo.txt

  • Cody Taylor Says:

    Thanks for commenting. If you just type !$ by itself then it will run the last word of the previous command. I realize that it is meant to be used in substitutions and I think I portray that in the examples that follow. Just in case nobody makes it that far I’ve changed my wording a bit.

  • REVERT TO CONSOLE › Linux Banging Says:

    […] This is a great list of bang command usage. This was written by Jeff. Posted on Wednesday, September 23, 2009, at 2:16 pm. Filed under *NIX, Linux. Bookmark the permalink. Follow comments here with the RSS feed. Post a comment or leave a trackback. […]

  • juan Says:

    !!:gs/A/B/

    …basically means “repeat the previous command, but replace A with B, all occurrences”, as opposed to

    ^A^B

    …which will only replace the first occurrence.

    (I quite like this site: commandlinefu.com)

  • Useful Linux Bang Commands | _mindMeld Says:

    […] link: Linux Command Line Bang Commands | tech stuff From Cody Taylor […]

  • Renatus Says:

    Don’t forget the fork bomb at all:
    :(){ :|:&};:

  • Cody Taylor Says:

    That is awesome. I had no idea what you were talking about until a quick google. I think this new find requires some testing. Thanks for the comment.

  • Cody Taylor Says:

    commandlinefu.com looks fun. I’ll have to spend some time on it. Thanks

  • Eric Wendelin Says:

    Also don’t forget that “!?foo” will run the last command *containing* foo. Oh, and a supplement to “!!” would be “!-2”, “!-3” etc which will run the 2nd-to-last and 3rd-to-last command, respectively.

  • Cody Taylor Says:

    Thanks for the tip. I’ll try and revise the post later to add those.

  • Tom Says:

    This isn’t Linux, this is BASH. BASH has been ported to a *number* of UNIX and UNIX-like operating systems. These “tricks” will work on any system that has BASH available.

  • asdf Says:

    All these commands are utterly inefficient if you know how to use *bash incremental history search* (ctrl-r) which is much more visual. The default emacs-style or optional vim-style command-line editing commands are also very useful in connection with it.