Quickly view & execute previous commands

Posted on

In Bash, you can execute the last command you ran starting with some prefix like so:

# executes a previous command starting with 'my-cmd'
$ !my-cmd

Oftentimes, you’re not sure you want to run that command without reviewing it first; it might be destructive or otherwise harmful. So, you can view it in fulll like so:

# echos 'my-cmd-in-full arg1 arg2 ...'
$ !my-cmd:p

Previewing your command has the useful side effect of registering it as the ‘last command’. That means, after a preview, you can quickly execute it with a double bang.

# preview command and then execute
$ !my-cmd:p
$ !!

Leave a Reply

Your email address will not be published.