If we’ve got this far, it’s time to talk about command substitution which is a very useful Bash shell 1
feature. Command substitution means nothing more than to run a shell command and store its output into 2
a variable for later use. If there are two identical ways to perform command substitution. 3
The first one is to write the command between backquotes or backticks. This is a special character 4
below 5
the ESC key on the keyboard; for example, now=`date` 6
And this is command substitution. The date command was executed and its output start into a new variable 7
called now; this variable can be later used inside the script. 8
echo $now 9
In fact, what has happened is that the data command was substituted or replaced by its output. And 10
because the output is a string value, it's even better to enclose everything between double quotes 11
like this. 12
It's better! The second way to perform command substitution is to write a dollar symbol and the command 13
between parentheses like this, users=” because the output of the comment will be 14
a string 15
the dollar symbol and the comment between parentheses and the command will be who 16
and echo $users 17
By the way, this is the same as u= 18
the command between double quotes and echo $u 19
It's the same! Of course, you can substitute the entire command line, which contains more commands 20
piped together. I'm searching in the output ps-ef for a process named bash ps-ef | grep 21
bash 22
And now I'm substituting the command and store its output to a variable. 23
So output equals and the common substitution. 24
$, the parentheses and the command. 25
echo and $output between double quotes 26
Let's try another useful example. I want to create a compressed archive of /etc and I want 27
to embed in the name of the resulting archive file the current date and time. 28
This is useful if you want to have versions of the backup files. 29
Let's see how we can display the data in an appropriate format. 30
Looking through the data command man page, we see that there are some formatters that control the output. 31
And %F will display the full date as year, month and day. 32
date +%F 33
And if you want store the hour and the minute as well, add % H and % M. 34
%H for the hour and %M for the minute like this. 35
An _%H%M, the year, the month, the day, the hour and 36
the minute. 37
This is the command that will be substituted when creating the archive 38
since we need the output. 39
First, let's create a compressed archive of /etc without any command substitution. 40
sudo tar -czvf etc.tar.gz and the directory 41
It is./etc/ 42
This is the resulting file, and if at a later time I execute the tar command again, then this archive 43
will be overwritten. 44
It has overwritten the archive, so the current date and time in the archive name makes a lot of sense. 45
Here, I'm going to substitute the date command we've just seen with its output, so $ the parentheses 46
and the command date+ %F. 47
%H%M and I am running the command again. 48
This is the archive. 49
We see the current date and time in the archive file name. If I run the same command again after at 50
least a minute, a new backup file will be created. 51
I'm waiting a few seconds. 52
OK,it is already 1: 56 and I'm running the command again. 53
And there's another archive, another file. 54
We'll take a short break, and in the following lecture, we'll discuss how to compare strings in Bash.