Quickly re-execute a recent command in bash

! will expand to the last time you ran , options and all. It's a nicer alternative to ^R for simple cases, and it's quite helpful for those long commands you run every now and then and haven't made aliases or functions for. It's similar to command 3966, in some sense.
Sample Output
$ mysql --sigint-ignore --host <host> --user <user> --password --database <db> -e 'select * from contacts' > contacts.tab
# several lines later, database has been updated, need to re-download the table
$ !mysql
mysql --sigint-ignore --host <host> --user <user> --password --database <db> -e 'select * from contacts' > contacts.tab

By: kaedenn
2011-08-16 18:37:18

These Might Interest You

  • I don't know why anyone would use this, I was just messing around tonight and managed to start bash without using any letters and thought I would share. It's pretty simple, first it tries to execute "-" redirecting stderr to stdout which prints the error "bash: -: command not found" to standard output, then I try to execute "bash: -: command not found" which produces the output "bash: bash: -: command not found: command not found". lastly, (on the other side of the semicolon) I use the underscore environment variable which refers to the last command run ("bash: -: command not found") and take out everything after the first ":" character using brace expressions and your left with "bash" Show Sample Output

    "$(- 2>&1)";${_%%:*}
    LinuxMan · 2012-12-29 09:21:09 2
  • Execute the most recent command containing search string. This differs from !string as that only refers to the most recent command starting with search string. Show Sample Output

    din7 · 2009-11-03 22:51:10 1
  • echo "ls" > script.bash; This is my script, a simple 'ls'. gpg -c script.bash; Here I encrypt and passord-protect my script. This creates file script.bash.gpg. cat script.bash.gpg | gpg -d --no-mdc-warning | bash Here I open file script.bash.gpg, decrypt it and execute it.

    echo "ls" > script.bash; gpg -c script.bash; cat script.bash.gpg | gpg -d --no-mdc-warning | bash
    betsubetsu · 2013-03-10 09:34:12 1
  • Any changes to BASH shell made in .bashrc will be active in the current terminal window from the moment you execute this command, ie. aliases, prompt settings etc. No need to restart terminal. (In BASH 'source' simile to 'eval' lets you generally execute any bunch of commands stacked in a text file).

    source ~/.bashrc
    knoppix5 · 2012-10-01 08:30:19 0
  • You can set the previous bash command as the terminal title by this command. Explanation: -trap assigns a command to execute at a given bash signal. -in the $BASH_COMMAND you find the last command -you can set the terminal title with the escape sequence: \e]0;this is the title\007 -to let the echo care about the backslashes give the -e to it Since trap is a built in bash command you find more informatin in 'man bash'for more Source: http://www.davidpashley.com/articles/xterm-titles-with-bash.html

    trap 'echo -e "\e]0;$BASH_COMMAND\007"' DEBUG
    Vereb · 2009-09-29 21:09:21 6
  • If you would like to edit a previous command, which might be long and complicated, you can use the fc (I think it stands for fix command). Invoke fc alone will edit the last command using the default editor (specified by $FCEDIT, $EDITOR, or emacs, in that order). After you make the changes in the editor, save and exit to execute that command. The fc command is more flexible than what I have described. Please 'man bash' for more information.

    fc [history-number]
    haivu · 2009-03-20 15:09:43 6

What Others Think

It's supposed to say "last time you ran [command]". I have no idea why angle brackets aren't escaped.
kaedenn · 352 weeks and 4 days ago
This is handy. I also dig being able to reuse a value from a previous command. maybe something like nslookup www.commandlinefu.com curl !!:1
gt · 352 weeks ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: