August 18, 2023

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.

See also:

  1. Source: $ man history↩︎

  2. Source: $ man bash↩︎

personal computing shell literacy command-line interface (cli) gnu linux trisquel history office applications wiki

No affiliate links, no analytics, no tracking, no cookies. © 2016-2023 Content is licensed under CC BY-NC-SA 4.0 .