1 00:00:00,720 --> 00:00:05,430 We are going to touch only briefly on a parameter expansion in this lecture. 2 00:00:05,610 --> 00:00:07,080 Let's clear the console first. 3 00:00:07,080 --> 00:00:11,100 So but we will be covering it extensively in the later just to mention that. 4 00:00:11,100 --> 00:00:16,410 So it's a feature that is more useful in shell scripts and directly on the command line. 5 00:00:16,410 --> 00:00:23,340 So many of the capabilities have to do with the system's ability to store small chunks of data and to 6 00:00:23,340 --> 00:00:25,380 give each chunk a name. 7 00:00:25,620 --> 00:00:33,090 Many such chunks more properly called R variables are available for your examination. 8 00:00:33,090 --> 00:00:41,460 So, for example, the variable user contains username to invoke parameter expansion and reveal the 9 00:00:41,460 --> 00:00:42,420 contents of user. 10 00:00:42,420 --> 00:00:47,700 You should do this echo dollar sign and user. 11 00:00:47,700 --> 00: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 12 00:00:55,560 --> 00:00:56,040 variables. 13 00:00:56,040 --> 00:01:01,780 You can also try this print any v here and last year we will use this. 14 00:01:01,780 --> 00:01:08,350 And as you can see here we have this system variables and which is called PG. 15 00:01:08,440 --> 00:01:08,950 You can. 16 00:01:08,950 --> 00:01:16,120 And the home directory is this our language log name is iPhone password or. 17 00:01:16,150 --> 00:01:17,680 No, it's actually PWD. 18 00:01:18,820 --> 00:01:25,450 Uh, here, uh, this is the working directory that we are in, and here we have, uh, information 19 00:01:25,450 --> 00:01:26,050 about that. 20 00:01:26,050 --> 00:01:30,310 So let's actually end this here. 21 00:01:31,300 --> 00:01:31,810 Oops. 22 00:01:33,200 --> 00:01:33,800 Here. 23 00:01:41,750 --> 00:01:48,980 Exit this and here with Ctrl Q or for example, and as you can say, press return lock file is already 24 00:01:48,980 --> 00:01:49,400 in use. 25 00:01:49,400 --> 00:01:55,790 So we are use this lock file and here we are going to exit it now. 26 00:01:58,530 --> 00:01:59,530 With controls. 27 00:01:59,910 --> 00:02:06,360 So you may have noticed that with other types of expansion and as you can see, we have our current 28 00:02:06,360 --> 00:02:07,860 working directory is p.w.d.. 29 00:02:07,950 --> 00:02:10,860 So we're going to clear and CD here. 30 00:02:10,860 --> 00:02:16,110 And again, CD, Let's go home here and let's and let's say we are in the home here. 31 00:02:16,110 --> 00:02:23,310 So as I said, if you noticed, with other types of expansion, if you mistype a pattern, the expansion 32 00:02:23,340 --> 00:02:27,960 will not take place and the echo command will simply display the mistyped pattern. 33 00:02:27,960 --> 00:02:33,780 So with parameter expansion, if you misspell the name of variable, the expansion will still take place 34 00:02:33,780 --> 00:02:37,590 but will result in an empty string. 35 00:02:37,590 --> 00:02:39,510 So for example, this. 36 00:02:39,510 --> 00:02:46,020 So as you remember in previous minutes of our lecture, we printed our username with user. 37 00:02:46,020 --> 00:02:51,060 But if we write it like that, Echo Sawyer here or yeah. 38 00:02:51,090 --> 00:02:55,950 Sawyer So actually it's a misspelled type of the user. 39 00:02:55,950 --> 00:02:56,550 WHEN Okay. 40 00:02:56,610 --> 00:03:02,320 And as you can see here, we got nothing as an error and it's just an new line. 41 00:03:02,320 --> 00:03:06,670 So you will also learn about the command substitution in this lecture. 42 00:03:06,670 --> 00:03:12,250 So common substitution allows us to the output of a command as an expansion. 43 00:03:12,250 --> 00:03:14,770 So in order to do that, we will echo. 44 00:03:15,430 --> 00:03:21,550 And here in the inside the braces we will use LHS and here. 45 00:03:21,550 --> 00:03:24,010 And as you can see, we got this command here. 46 00:03:24,010 --> 00:03:35,260 So and one of my favorites is this here which and CP and as you can see here, we got this. 47 00:03:35,260 --> 00:03:44,530 So here we pass the results of which CP as an argument to the command, thereby getting the list of 48 00:03:44,790 --> 00:03:48,670 a list of the CP program without to know its full path name. 49 00:03:48,670 --> 00:03:52,510 So we are not limited, just a simple commands entire pipe. 50 00:03:52,510 --> 00:03:57,310 Pipelines can be used for example, and working with it. 51 00:03:57,310 --> 00:03:59,380 We also have the cool thing here. 52 00:03:59,380 --> 00:04:05,680 So now that we have seen how many ways the Shell can perform expansions, it's time to learn how we 53 00:04:05,680 --> 00:04:06,730 can control it. 54 00:04:06,730 --> 00:04:09,790 So take for example, this echo. 55 00:04:09,820 --> 00:04:14,800 This is Oxley and oops, not like this. 56 00:04:14,830 --> 00:04:17,620 Oh, this is Oxley Academy. 57 00:04:17,620 --> 00:04:21,160 And as you can see here, this ignored all of these spaces here. 58 00:04:21,160 --> 00:04:26,290 And you can also do, for example, echo the total. 59 00:04:27,130 --> 00:04:28,270 Total. 60 00:04:29,050 --> 00:04:30,100 Price. 61 00:04:30,460 --> 00:04:31,050 Price. 62 00:04:32,730 --> 00:04:36,270 Price is, for example, $100. 63 00:04:36,270 --> 00:04:40,810 And here and as you can see here, the total price is zero. 64 00:04:40,830 --> 00:04:50,550 So in this first example with this word splitting by the shell, removed extra whitespace from the Echo 65 00:04:50,550 --> 00:04:53,370 command and the echo commands list of arguments. 66 00:04:53,370 --> 00:05:01,380 So in the second example, parameter expansion substitute an empty string for the value of $101 because 67 00:05:01,380 --> 00:05:03,510 it was an undefined variable. 68 00:05:03,510 --> 00:05:10,650 So the shell provides a mechanism called quoting to selectively suppress unwanted expressions. 69 00:05:10,650 --> 00:05:15,180 Here we also have the double quotes here, double quotes here. 70 00:05:15,180 --> 00:05:21,480 So the first type of quoting we will look at is double quotes. 71 00:05:22,480 --> 00:05:29,590 If a place text inside the double quotes, all the special characters used by the shell lose their special 72 00:05:29,590 --> 00:05:32,590 meaning and are treated as an ordinary characters. 73 00:05:32,590 --> 00:05:40,150 So the expressions are the dollar sign and backslash and backtick here. 74 00:05:40,180 --> 00:05:41,100 Backtick. 75 00:05:41,110 --> 00:05:47,530 So this means that the word splitting pathname expansion, titled expansion and brace expansions are 76 00:05:47,530 --> 00:05:48,280 suppressed. 77 00:05:48,280 --> 00:05:53,920 In this case, however, parameter expansion, arithmetic expansion and common substitution are still 78 00:05:53,920 --> 00:06:00,110 carried out Using double quotes, we can cope with file names containing embedded spaces. 79 00:06:00,130 --> 00:06:04,740 Let's say we were the unfortunate victim of a file called this. 80 00:06:04,750 --> 00:06:09,460 Two words dot txt here. 81 00:06:11,080 --> 00:06:19,650 If we try to use this on the command line, we are word splitting wood lines called the two words txt. 82 00:06:19,660 --> 00: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 83 00:06:25,450 --> 00:06:28,390 rather than the desired single argument. 84 00:06:28,390 --> 00:06:32,140 So for example, I want to create a new file, for example. 85 00:06:32,590 --> 00:06:37,780 Torch torch my file without question mark or something. 86 00:06:37,780 --> 00:06:44,050 And here and as you can see, we created my file, which now if we write it like that, for example, 87 00:06:44,080 --> 00:06:49,150 ls l my file my file dot txt. 88 00:06:49,300 --> 00:06:56,140 This will take as a two arguments, but in order to do access this my file we will use double quotes 89 00:06:56,140 --> 00:06:58,030 for accessing this here. 90 00:06:58,030 --> 00:07:02,350 And as you can see here we access to my file here. 91 00:07:04,000 --> 00:07:04,900 So there. 92 00:07:05,170 --> 00:07:09,570 And as you can see now, we don't have to keep tying those pesky double quotes. 93 00:07:09,570 --> 00:07:15,970 So remember, parameter expansions, arithmetic expansions and common substitution still take place 94 00:07:15,970 --> 00:07:17,180 within the double quotes. 95 00:07:17,180 --> 00:07:18,880 So for example, look at this. 96 00:07:18,880 --> 00:07:22,990 So echo and here we will do double quotes. 97 00:07:22,990 --> 00:07:25,690 So inside this double quotes we will user. 98 00:07:27,020 --> 00:07:29,900 And dollar sign to. 99 00:07:31,020 --> 00:07:31,980 The last two. 100 00:07:32,850 --> 00:07:33,930 Two plus two. 101 00:07:34,440 --> 00:07:37,590 And after that we will use a dollar sign. 102 00:07:37,590 --> 00:07:39,390 And after that we will call calendar. 103 00:07:39,390 --> 00:07:45,330 So this call is actually calendar, which we will show us and calendar is actually not installed. 104 00:07:45,330 --> 00:07:47,580 So let's actually install calendar. 105 00:07:47,580 --> 00: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 106 00:07:57,900 --> 00:07:58,140 is just. 107 00:07:59,280 --> 00:07:59,820 Here. 108 00:08:00,780 --> 00:08:01,680 And here. 109 00:08:01,680 --> 00:08:06,450 This is just a 69kB of file size, and it's already installed here. 110 00:08:06,450 --> 00:08:09,950 So now let's clear our files. 111 00:08:09,960 --> 00:08:12,630 Clear and use previous here. 112 00:08:12,630 --> 00:08:16,950 And as you can see here, we got calendar on it on the screen. 113 00:08:16,950 --> 00:08:22,440 So we shall take a moment to look at the effect of double quotes on common substitutions. 114 00:08:22,440 --> 00:08:27,780 So first, let's look a little deeper at words, how words splitting works. 115 00:08:27,780 --> 00:08:35,730 In earlier lectures, we saw how words splitting appears to remove extra spaces of in our text. 116 00:08:35,730 --> 00:08:42,090 For example, this is this is Oxley and Here Academy. 117 00:08:42,090 --> 00:08:44,760 And as you can see here, it just ignored our text. 118 00:08:44,760 --> 00:08:51,090 So by default, the word splitting looks for the presence of a space tabs and newlines in line characters 119 00:08:51,090 --> 00:08:56,130 line feed characters and actually and treats them as delimiters between words. 120 00:08:56,130 --> 00:09:02,950 So this means unquoted space tabs and newlines are not considered to be part of the text, so they serve 121 00:09:02,950 --> 00:09:07,360 only as separators because they separate the words into different arguments. 122 00:09:07,390 --> 00:09:12,430 Our example command line contains a comment followed by for district arguments. 123 00:09:12,460 --> 00:09:20,710 If we add like this, for example, add the quotation marks in the start of the end line and as you 124 00:09:20,710 --> 00:09:23,740 can see here, it is not ignoring our. 125 00:09:24,680 --> 00:09:32,490 The new lines or spaces as it did in our previous, like previous example.