MacOS – How to pass a terminal command as an argument to another command


I want to run this command:

ls -arth Test*log | tail -1

… and pass its output as an argument to:

open /Applications/Utilities/ <previous output>

I would like to do this all in one line.

Best Answer

This is known as command substitution:

open /Applications/Utilities/ "$(ls -arth Test*log | tail -1)"

If you're certain that the output of the command you're substituting won't contain spaces or newlines (or if you want to supply space/newline-separated output as multiple arguments), you can omit the quotes:

open /Applications/Utilities/ $(ls -arth Test*log | tail -1)