Skip to main content

Command Palette

Search for a command to run...

Command Substitution

Updated
•1 min read•View as Markdown
Command Substitution

In Bash, we can save the command line output of a command to a variable.

This can be done by $ ( command )

dir=.
count=$(ls $dir | wc -l)
echo "There are $count files at location $dir"
# There are 4 files at location .

In the above example, we count no of files present in a directory with the command ls $dir | wc -l and then store this number in the variable count

Note:

If the output of the command has multiple lines then the line breaks are replaced with spaces with command substitution.