- [Instructor] We can insert a variable into
a text string or command line and have
the variable value substituted for the variable name.
For instance, in a terminal type echo, space,
double quote, my space name, space, is, space,
dollar sign, capital USER, double quote and hit enter.
This outputs the text my name is
followed by the value and the user variable.
As we've seen elsewhere in this course,
we need to be careful with quoting.
In order to have variable substitution,
we need to use double quotes.
This echo command is pretty basic.
Now let's use a command to process
the value of a variable.
To set this up, let's create a couple of variables.
In a terminal, type in PDIR equals double quote
slash TMP, slash files, slash today,
double quote and hit enter.
Let's create a second variable called fname.
Fname equals double quote, report, double quote.
And hit enter.
Variable names can be uppercase or lowercase.
Most system variables are uppercase.
Now let's use these variables in a command.
Type in mkdir, space, dash P, space,
dollar sign, PDIR and hit enter.
Make dir-P makes the directory and
any parent directories if needed.
Let's verify this with LS-D.
Type in LS, space dash D, space,
dollar sign, PDIR and hit enter.
We need the dash D option so LS
knows to list the directory and not the contents.
Now let's use touch to create a file using variables.
Type in touch, space, dollar sign,
PDIR, slash dollar sign, fname, and hit enter.
This expands out to touch, space,
slash TMP, slash files, slash today, slash report.
Verify this with LS.
Type in LS space dash L, space dollar sign,
PDIR, slash dollar sign fname and hit enter.
This shows that the /tmp/files/today/report file exists.
Things get a bit trickier when you add additional text.
Let's say I wanted to create a file
called report_January.
I may type in touch, space, dollar sign pdir
slash dollar sign fname_January.
And then I would verify it with LS dollar sign
pdir slash dollar sign, fname, underscore, January
and hit enter.
Not only did this not list our file named
report_January, it listed the parent directory.
The reason for this is we thought we were adding
_January to the value of the variable
and Dash added it to the variable name.
It thinks we wanted to create a file with the path
/tmp/files/today followed by the value of
the variable called dollar sign fname_January.
The problem is, this variable doesn't exist yet
so it's empty.
In our case we need to preserve the variable's name.
To preserve the variable's name,
we can place curly braces around it.
Type in touch, space, $pdir/
dollar sign, left curly bracket,
fname, right curly bracket, _January and hit enter.
Now let's verify this with LS-L.
Type in LS-L, space, $pdir, slash,
dollar sign, left curly bracket, fname,
right curly bracket, _January and hit enter.
And we see that it created the file.
Placing quotes around our strings
and braces around variable names
makes Dash variables much more reliable.
For instance, typing in LS space dash L,
space, double quote, dollar sign,
left curly bracket, pdir, right curly bracket,
slash, dollar sign, left curly bracket,
fname, right curly bracket, _January,
double quote, ends up being very reliable.
We can also insert commands into our strings
and have them executed during command substitution.
When the command is executed, the standard output
of the command is inserted into its place.
Let's type in clear to clear our screen
and then I'll give you an example.
Type in echo, space, double quote,
my, space, name, space, is, space,
dollar sign, left parentheses, whoami, one word,
right parentheses, double quote, and hit enter.
This is a very simple example that runs
the whoami command first and then substitutes
the output in place of the command.
When we do command substitution, you can either
surround the command with back ticks
or a dollar sign in parentheses as we've done.
An example using back ticks would be echo, space,
double quote, my, space, name, space, is,
space, back tick, whoami, back tick, double quote.
This does the same thing as the dollar sign
parentheses but it isn't as easy to nest.
To do nesting with a dollar sign and parentheses,
type in echo, space, double quote,
permissions for find are, space, dollar sign,
left parentheses, LS space dash L,
space, dollar sign, left parentheses,
which, space, find, right parentheses,
right parentheses, double quote.
Hit enter.
Let's analyze what is happening here.
The output of which find shows which directory
the find command resides in, which is /bin/find.
That output is then inserted into the outer
command substitution and then executed.
The outer command substitution uses the LS
command to get the permissions of the find command,
which is then inserted into the whole line
and executed again, resulting in a sentence
consisting of a string of characters
followed by the permissions for find.
You can nest command substitutions easily
with a dollar sign and parentheses combination.