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.

Categories
Resources Software Development

BASH shell scripting links and resources

Bash scripting manual 5.1

Advanced Bash scripting guide

Bash script cheat sheet

Bash Scripting: Everything you need to know about Bash-shell programming

Respond to prompt in Bash Script – a tutorial on how to respond to prompts in Bash scripts.

How To Write a Bash Script That Answers Interactive PromptsĀ 

MySQL: Run Query from Bash Script or Linux Command Line

How to Use Variables in Shell Scripting

Bash scripts and Mysql – a good article about working with Mysql in bash scripts, how to load SQL etc.

If statement syntax example coverage

More coverage and examples of Bash if/else statement syntax and use

Bash read builtin command used to prompt the user for a response and store it for use in the script

How to prompt for input from user in a linux shell script


How to prompt and read user input in a Bash shell script – more examples and information. Different from the above link.