Ubuntu – use ‘!!’ in aliases or scripts

aliasbashcommand line

In the Terminal I can use !! which will reference my previous command.
Is it possible to use this in an alias? I have tried it, but what happens is that the double exclamation marks are interpreted literally.

What I want to have possible is:

  1. I enter some command
  2. I type an alias that will include what was entered in step 1

If there is a solution using scripts that is also acceptable.

Best Answer

The command that lists the last executed command is fc -nl -1 . Using output substitution , we can add more parameters to the same content

$> ls /etc/passwd
/etc/passwd
$> $(fc -nl -1)  /etc/group                                                     
/etc/group  /etc/passwd
$> 

Quoting , however, may be an issue with this approach

A very nice feature of fc is that if you just run fc command by itself, it will open text editor specified inFCEDIT variable (which you probably want stored in ~/.bashrc) and the contents of the line will be your last command. For your ease, I suggest you use nano as your text editor, but if you know vim - even better.

For example, what if I need to edit qdbus org.ayatana.bamf /org/ayatana/bamf/matcher org.ayatana.bamf.matcher.ActiveWindow Huge line, right ? But with fc, I can open vim and edit /org/ayatana/bamf/matcher , save, exit and it will run.