Please enable JavaScript eh!

 Web Mechanic 

Bash Scripting


Command Substitution

This is a very useful feature to understand, as it will allow us to create some good tools.

Many times in programming, we want to capture the output of a command, and use it later. As an example, we want to capture today's date, and enter it into a database.

It's easy enough to get the date while working on the command line:

date
Tue Mar 17 13:11:13 EDT 2026

To get that result into a variable, is what this is all about.

Today=date
echo $Today
date

echo '$Today'
$Today

echo "$Today"
date

That didn't work. In order to get the result of a command into a variable, use back-ticks around the command:

Today=`date`
echo $Today
Tue Mar 17 13:24:28 EDT 2026

echo '$Today'
$Today

echo "$Today"
Tue Mar 17 13:24:28 EDT 2026

As mentioned previously, it is NOT best practice to use back-ticks, because they can easily be interpreted as single quotes.

Instead, another format is better:

Today=$(date)
echo "$Today"
Tue Mar 17 13:50:09 EDT 2026

echo $Today
Tue Mar 17 13:50:09 EDT 2026

echo '$Today'
$Today