Pass the output of the last command as an argument to the next
$ command $(!!)
will pass the output of the last command to the newly typed command.
First the event designator
!! repeats the last command1. Then command substitution, i.e.
$(), performs the expansion by executing the command in a subshell environment and replaces it by its output2, thus passing it as an argument to the newly typed command.
For example, if you want to open a pdf document but don’t know its path, you can first use
find to locate the document and print the path to the shell, and then use the event designator
!! and command substitution to recall the path to that document.
$ find $HOME -name document.pdf /home/username/Documents/projects/subdirectory/document.pdf # a long path $ okular $(!!)
okular is just the name of a copyleft-licensed document viewer.
- Find files or directories from the command-line with find
- Copy a command from the shell history to the clipboard
- Copy a command from the shell history to a file