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. This work © 2016-2024 by yctct is licensed under CC BY-ND 4.0 .   about me   contact me   all entries & tags   FAQ   GPG public key

GPG fingerprint: 2E0F FB60 7FEF 11D0 FB45 4DDC E979 E52A 7036 7A88