WEBVTT

00:00.720 --> 00:05.430
We are going to touch only briefly on a parameter expansion in this lecture.

00:05.610 --> 00:07.080
Let's clear the console first.

00:07.080 --> 00:11.100
So but we will be covering it extensively in the later just to mention that.

00:11.100 --> 00:16.410
So it's a feature that is more useful in shell scripts and directly on the command line.

00:16.410 --> 00:23.340
So many of the capabilities have to do with the system's ability to store small chunks of data and to

00:23.340 --> 00:25.380
give each chunk a name.

00:25.620 --> 00:33.090
Many such chunks more properly called R variables are available for your examination.

00:33.090 --> 00:41.460
So, for example, the variable user contains username to invoke parameter expansion and reveal the

00:41.460 --> 00:42.420
contents of user.

00:42.420 --> 00:47.700
You should do this echo dollar sign and user.

00:47.700 --> 00:55.560
And here, as you can see here, I printed my username so to list of the to see a list of available

00:55.560 --> 00:56.040
variables.

00:56.040 --> 01:01.780
You can also try this print any v here and last year we will use this.

01:01.780 --> 01:08.350
And as you can see here we have this system variables and which is called PG.

01:08.440 --> 01:08.950
You can.

01:08.950 --> 01:16.120
And the home directory is this our language log name is iPhone password or.

01:16.150 --> 01:17.680
No, it's actually PWD.

01:18.820 --> 01:25.450
Uh, here, uh, this is the working directory that we are in, and here we have, uh, information

01:25.450 --> 01:26.050
about that.

01:26.050 --> 01:30.310
So let's actually end this here.

01:31.300 --> 01:31.810
Oops.

01:33.200 --> 01:33.800
Here.

01:41.750 --> 01:48.980
Exit this and here with Ctrl Q or for example, and as you can say, press return lock file is already

01:48.980 --> 01:49.400
in use.

01:49.400 --> 01:55.790
So we are use this lock file and here we are going to exit it now.

01:58.530 --> 01:59.530
With controls.

01:59.910 --> 02:06.360
So you may have noticed that with other types of expansion and as you can see, we have our current

02:06.360 --> 02:07.860
working directory is p.w.d..

02:07.950 --> 02:10.860
So we're going to clear and CD here.

02:10.860 --> 02:16.110
And again, CD, Let's go home here and let's and let's say we are in the home here.

02:16.110 --> 02:23.310
So as I said, if you noticed, with other types of expansion, if you mistype a pattern, the expansion

02:23.340 --> 02:27.960
will not take place and the echo command will simply display the mistyped pattern.

02:27.960 --> 02:33.780
So with parameter expansion, if you misspell the name of variable, the expansion will still take place

02:33.780 --> 02:37.590
but will result in an empty string.

02:37.590 --> 02:39.510
So for example, this.

02:39.510 --> 02:46.020
So as you remember in previous minutes of our lecture, we printed our username with user.

02:46.020 --> 02:51.060
But if we write it like that, Echo Sawyer here or yeah.

02:51.090 --> 02:55.950
Sawyer So actually it's a misspelled type of the user.

02:55.950 --> 02:56.550
WHEN Okay.

02:56.610 --> 03:02.320
And as you can see here, we got nothing as an error and it's just an new line.

03:02.320 --> 03:06.670
So you will also learn about the command substitution in this lecture.

03:06.670 --> 03:12.250
So common substitution allows us to the output of a command as an expansion.

03:12.250 --> 03:14.770
So in order to do that, we will echo.

03:15.430 --> 03:21.550
And here in the inside the braces we will use LHS and here.

03:21.550 --> 03:24.010
And as you can see, we got this command here.

03:24.010 --> 03:35.260
So and one of my favorites is this here which and CP and as you can see here, we got this.

03:35.260 --> 03:44.530
So here we pass the results of which CP as an argument to the command, thereby getting the list of

03:44.790 --> 03:48.670
a list of the CP program without to know its full path name.

03:48.670 --> 03:52.510
So we are not limited, just a simple commands entire pipe.

03:52.510 --> 03:57.310
Pipelines can be used for example, and working with it.

03:57.310 --> 03:59.380
We also have the cool thing here.

03:59.380 --> 04:05.680
So now that we have seen how many ways the Shell can perform expansions, it's time to learn how we

04:05.680 --> 04:06.730
can control it.

04:06.730 --> 04:09.790
So take for example, this echo.

04:09.820 --> 04:14.800
This is Oxley and oops, not like this.

04:14.830 --> 04:17.620
Oh, this is Oxley Academy.

04:17.620 --> 04:21.160
And as you can see here, this ignored all of these spaces here.

04:21.160 --> 04:26.290
And you can also do, for example, echo the total.

04:27.130 --> 04:28.270
Total.

04:29.050 --> 04:30.100
Price.

04:30.460 --> 04:31.050
Price.

04:32.730 --> 04:36.270
Price is, for example, $100.

04:36.270 --> 04:40.810
And here and as you can see here, the total price is zero.

04:40.830 --> 04:50.550
So in this first example with this word splitting by the shell, removed extra whitespace from the Echo

04:50.550 --> 04:53.370
command and the echo commands list of arguments.

04:53.370 --> 05:01.380
So in the second example, parameter expansion substitute an empty string for the value of $101 because

05:01.380 --> 05:03.510
it was an undefined variable.

05:03.510 --> 05:10.650
So the shell provides a mechanism called quoting to selectively suppress unwanted expressions.

05:10.650 --> 05:15.180
Here we also have the double quotes here, double quotes here.

05:15.180 --> 05:21.480
So the first type of quoting we will look at is double quotes.

05:22.480 --> 05:29.590
If a place text inside the double quotes, all the special characters used by the shell lose their special

05:29.590 --> 05:32.590
meaning and are treated as an ordinary characters.

05:32.590 --> 05:40.150
So the expressions are the dollar sign and backslash and backtick here.

05:40.180 --> 05:41.100
Backtick.

05:41.110 --> 05:47.530
So this means that the word splitting pathname expansion, titled expansion and brace expansions are

05:47.530 --> 05:48.280
suppressed.

05:48.280 --> 05:53.920
In this case, however, parameter expansion, arithmetic expansion and common substitution are still

05:53.920 --> 06:00.110
carried out Using double quotes, we can cope with file names containing embedded spaces.

06:00.130 --> 06:04.740
Let's say we were the unfortunate victim of a file called this.

06:04.750 --> 06:09.460
Two words dot txt here.

06:11.080 --> 06:19.650
If we try to use this on the command line, we are word splitting wood lines called the two words txt.

06:19.660 --> 06:25.450
And if we try to use this on a command, word splitting will cause this to be treated as a two arguments

06:25.450 --> 06:28.390
rather than the desired single argument.

06:28.390 --> 06:32.140
So for example, I want to create a new file, for example.

06:32.590 --> 06:37.780
Torch torch my file without question mark or something.

06:37.780 --> 06:44.050
And here and as you can see, we created my file, which now if we write it like that, for example,

06:44.080 --> 06:49.150
ls l my file my file dot txt.

06:49.300 --> 06:56.140
This will take as a two arguments, but in order to do access this my file we will use double quotes

06:56.140 --> 06:58.030
for accessing this here.

06:58.030 --> 07:02.350
And as you can see here we access to my file here.

07:04.000 --> 07:04.900
So there.

07:05.170 --> 07:09.570
And as you can see now, we don't have to keep tying those pesky double quotes.

07:09.570 --> 07:15.970
So remember, parameter expansions, arithmetic expansions and common substitution still take place

07:15.970 --> 07:17.180
within the double quotes.

07:17.180 --> 07:18.880
So for example, look at this.

07:18.880 --> 07:22.990
So echo and here we will do double quotes.

07:22.990 --> 07:25.690
So inside this double quotes we will user.

07:27.020 --> 07:29.900
And dollar sign to.

07:31.020 --> 07:31.980
The last two.

07:32.850 --> 07:33.930
Two plus two.

07:34.440 --> 07:37.590
And after that we will use a dollar sign.

07:37.590 --> 07:39.390
And after that we will call calendar.

07:39.390 --> 07:45.330
So this call is actually calendar, which we will show us and calendar is actually not installed.

07:45.330 --> 07:47.580
So let's actually install calendar.

07:47.580 --> 07:57.900
So app sudo apt apt, install ankle and I don't think it will take much time because it's I think it's

07:57.900 --> 07:58.140
is just.

07:59.280 --> 07:59.820
Here.

08:00.780 --> 08:01.680
And here.

08:01.680 --> 08:06.450
This is just a 69kB of file size, and it's already installed here.

08:06.450 --> 08:09.950
So now let's clear our files.

08:09.960 --> 08:12.630
Clear and use previous here.

08:12.630 --> 08:16.950
And as you can see here, we got calendar on it on the screen.

08:16.950 --> 08:22.440
So we shall take a moment to look at the effect of double quotes on common substitutions.

08:22.440 --> 08:27.780
So first, let's look a little deeper at words, how words splitting works.

08:27.780 --> 08:35.730
In earlier lectures, we saw how words splitting appears to remove extra spaces of in our text.

08:35.730 --> 08:42.090
For example, this is this is Oxley and Here Academy.

08:42.090 --> 08:44.760
And as you can see here, it just ignored our text.

08:44.760 --> 08:51.090
So by default, the word splitting looks for the presence of a space tabs and newlines in line characters

08:51.090 --> 08:56.130
line feed characters and actually and treats them as delimiters between words.

08:56.130 --> 09:02.950
So this means unquoted space tabs and newlines are not considered to be part of the text, so they serve

09:02.950 --> 09:07.360
only as separators because they separate the words into different arguments.

09:07.390 --> 09:12.430
Our example command line contains a comment followed by for district arguments.

09:12.460 --> 09:20.710
If we add like this, for example, add the quotation marks in the start of the end line and as you

09:20.710 --> 09:23.740
can see here, it is not ignoring our.

09:24.680 --> 09:32.490
The new lines or spaces as it did in our previous, like previous example.
