Categories
Software Development Web Development

Linux bash scripting command substitution aka $(command)

Linux has this syntax that looks like so:
$(command)

This is called command substitution. This allows you to get information about the execution of the command instead of having it it directed to STDOUT aka the terminal screen as usual.

That is very useful actually because you can run a command and store the output in a variable and use it anywhere you want later.

A simple example you can easily play with:

DIR_LISTINGS=$(ls -al)
echo $DIR_LISTINGS

This is so simple you don’t even have to add it to a script you can run it straight in your terminal in any directory you user owns.

More information can be found in this excellent book Linux command line and shell scripting bible page 277

More info about command substitution.

Bash manual reference.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.