Using Command Substitution

You can use the output of one command when invoking another command. This is handy when a program’s argument needs to be generated from another program’s output. You can do this with a feature called command substitution. You’ve already seen variable expansion with curly braces, like ${variable}, where the variable’s value is expanded. Command substitution works in a similar way. With command substitution, the output of the command replaces the command itself. To define a command substitution, you wrap the command in parentheses instead of curly braces.

You saw an example of command substitution in Building Your Prompt, when you used the tput command to emit color codes:

 $ ​​echo​​ ​​-e​​ ​​"$(tput setaf 6)Hello ...

Get Small, Sharp Software Tools now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.