1 00:00:06,510 --> 00:00:09,540 - In this video, you will learn about variables. 2 00:00:09,540 --> 00:00:11,160 Now, what is a variable? 3 00:00:11,160 --> 00:00:13,740 A variable is a label to which the dynamic value 4 00:00:13,740 --> 00:00:15,210 can be assigned. 5 00:00:15,210 --> 00:00:16,230 All right. 6 00:00:16,230 --> 00:00:17,550 How is it useful? 7 00:00:17,550 --> 00:00:20,790 Now, variables are very convenient for scripting. 8 00:00:20,790 --> 00:00:22,650 You can define the variable once, 9 00:00:22,650 --> 00:00:27,360 and use it in a flexible way in different environments. 10 00:00:27,360 --> 00:00:32,360 Variables also generically are used to store information 11 00:00:32,580 --> 00:00:33,993 that is site-specific. 12 00:00:36,120 --> 00:00:38,370 The thing is you can write a script 13 00:00:38,370 --> 00:00:41,100 or write an operating system. 14 00:00:41,100 --> 00:00:43,950 The script or operating system works with specific 15 00:00:43,950 --> 00:00:46,530 information like your username, for instance. 16 00:00:46,530 --> 00:00:48,750 And if you want to write a script that is 17 00:00:48,750 --> 00:00:50,730 printing your username, how are you going to 18 00:00:50,730 --> 00:00:54,300 print a username while you store the username in a variable? 19 00:00:54,300 --> 00:00:57,420 And then you write a script that is just referring 20 00:00:57,420 --> 00:00:58,253 to the variable. 21 00:00:58,253 --> 00:01:00,390 And no matter what your username is, 22 00:01:00,390 --> 00:01:03,300 it will always use the right variable. 23 00:01:03,300 --> 00:01:07,040 In programming, we call that the option to separate 24 00:01:07,040 --> 00:01:10,710 the site specific from the static information, 25 00:01:10,710 --> 00:01:12,513 also known as decoupling. 26 00:01:13,350 --> 00:01:15,270 In Linux, you have system variables. 27 00:01:15,270 --> 00:01:17,430 System variables contain default settings 28 00:01:17,430 --> 00:01:19,170 that are used by Linux. 29 00:01:19,170 --> 00:01:21,453 A very common variable is PATH. 30 00:01:21,453 --> 00:01:25,020 PATH contains a list of directories, which are searched 31 00:01:25,020 --> 00:01:27,360 for binaries while executing a command. 32 00:01:27,360 --> 00:01:29,250 There's also a shell, which is a current shell 33 00:01:29,250 --> 00:01:30,093 that is used. 34 00:01:30,990 --> 00:01:34,470 Environment variables can be set for application use. 35 00:01:34,470 --> 00:01:39,060 And in order to set them, you use varname=value. 36 00:01:39,060 --> 00:01:41,580 That is what you do on your prompt. 37 00:01:41,580 --> 00:01:45,870 And if you do that, it's known to the current shell only. 38 00:01:45,870 --> 00:01:48,180 To refer to the value in the variable, 39 00:01:48,180 --> 00:01:50,040 you use echo, for instance. 40 00:01:50,040 --> 00:01:54,480 Or better you use command dollar and then the variable name. 41 00:01:54,480 --> 00:01:57,450 So to define a variable, there's no dollar in front 42 00:01:57,450 --> 00:01:58,950 of the name of the variable. 43 00:01:58,950 --> 00:02:02,160 To use the variable, there's a dollar in front of the name. 44 00:02:02,160 --> 00:02:03,780 As you can see in these two lines, 45 00:02:03,780 --> 00:02:05,883 varname is value echo $varname. 46 00:02:05,883 --> 00:02:07,233 That is how it works. 47 00:02:08,070 --> 00:02:11,940 But as I mentioned, variables are by default only known 48 00:02:11,940 --> 00:02:13,020 to the current shell. 49 00:02:13,020 --> 00:02:16,080 And if you want your variables to be available in all 50 00:02:16,080 --> 00:02:18,840 the sub-shells as well, that is in all the commands 51 00:02:18,840 --> 00:02:22,800 if you are going to run, from the shell, you use export. 52 00:02:22,800 --> 00:02:23,943 Let me demonstrate. 53 00:02:27,180 --> 00:02:31,350 So let me start by using echo $COLOR. 54 00:02:31,350 --> 00:02:33,990 I like typing my variables in uppercase, by the way, 55 00:02:33,990 --> 00:02:35,100 to make it more readable. 56 00:02:35,100 --> 00:02:38,370 You can do it in lowercase as well, as long as you realize 57 00:02:38,370 --> 00:02:40,380 that it is case sensitive. 58 00:02:40,380 --> 00:02:42,480 I'm using color is red, 59 00:02:42,480 --> 00:02:46,480 then I'm using echo $COLOR 60 00:02:49,320 --> 00:02:50,280 And what do we get? 61 00:02:50,280 --> 00:02:51,870 We get red. 62 00:02:51,870 --> 00:02:55,680 But now this thing about sub-shells, I am typing bash. 63 00:02:55,680 --> 00:02:56,550 What is happening? 64 00:02:56,550 --> 00:02:58,890 Well, I'm opening a shell within my shell. 65 00:02:58,890 --> 00:03:00,630 So that's a new environment. 66 00:03:00,630 --> 00:03:04,020 And if I'm using echo $COLOR from the sub-shell, 67 00:03:04,020 --> 00:03:05,400 I get nothing. 68 00:03:05,400 --> 00:03:07,710 I type exit to close my sub-shell. 69 00:03:07,710 --> 00:03:09,930 And I use my echo $COLOR again. 70 00:03:09,930 --> 00:03:13,980 And now we can see that I have access to my variable again. 71 00:03:13,980 --> 00:03:16,800 If I want my variable to exist in sub-shells, 72 00:03:16,800 --> 00:03:18,330 I need to use export. 73 00:03:18,330 --> 00:03:21,120 So export color is green, for instance. 74 00:03:21,120 --> 00:03:24,150 echo $COLOR, color is set to green. 75 00:03:24,150 --> 00:03:28,233 And if I type bash echo $COLOR, 76 00:03:30,360 --> 00:03:33,150 color is set to green in the sub cell as well. 77 00:03:33,150 --> 00:03:34,650 And that is how it is working. 78 00:03:35,850 --> 00:03:38,040 Now, variables can be set by yourself 79 00:03:38,040 --> 00:03:40,590 but more important are the variables that are a part 80 00:03:40,590 --> 00:03:42,420 of your operating system. 81 00:03:42,420 --> 00:03:45,630 If you use the env command, env for environment, 82 00:03:45,630 --> 00:03:48,870 then you see these so-called environment variables. 83 00:03:48,870 --> 00:03:51,930 And these environment variables are set in different ways. 84 00:03:51,930 --> 00:03:54,840 They can be set by the commands that you are using. 85 00:03:54,840 --> 00:03:59,400 They can be set by the startup shells of your shell. 86 00:03:59,400 --> 00:04:01,680 They can be set in different ways. 87 00:04:01,680 --> 00:04:04,950 You might wanna have a look at env pipeline tools 88 00:04:04,950 --> 00:04:07,830 and you can see all these different environment variables 89 00:04:07,830 --> 00:04:12,360 that are being set including variables 90 00:04:12,360 --> 00:04:14,850 like the PATH variable. 91 00:04:14,850 --> 00:04:17,880 The PATH variable actually is a pretty useful variable. 92 00:04:17,880 --> 00:04:19,530 It prints the path. 93 00:04:19,530 --> 00:04:23,040 And this PATH is where your Linux distribution 94 00:04:23,040 --> 00:04:26,790 is going to look for executable commands. 95 00:04:26,790 --> 00:04:27,630 And you know what? 96 00:04:27,630 --> 00:04:30,990 Let me just give you a very simple example. 97 00:04:30,990 --> 00:04:33,980 I'm using echo hello world > hello.sh. 98 00:04:39,120 --> 00:04:42,870 hello.sh, the extension .sh reveals that this is 99 00:04:42,870 --> 00:04:45,030 supposed to be shell script. 100 00:04:45,030 --> 00:04:47,283 Shell scripts need to be executable, 101 00:04:49,489 --> 00:04:53,310 and I am using chmod +x hello.sh to give it 102 00:04:53,310 --> 00:04:55,080 the execute permission. 103 00:04:55,080 --> 00:04:58,050 We can verify hello.sh. 104 00:04:58,050 --> 00:05:00,420 There we go. It has the execute permission. 105 00:05:00,420 --> 00:05:03,690 I will tell you all about this in Lesson 10, by the way. 106 00:05:03,690 --> 00:05:05,562 This is just a quick demo. 107 00:05:05,562 --> 00:05:08,820 What I want you to see is if I type hello.sh, 108 00:05:08,820 --> 00:05:10,860 I am getting command not found. 109 00:05:10,860 --> 00:05:11,850 Why is it? 110 00:05:11,850 --> 00:05:14,700 Because the current directory is not in the PATH. 111 00:05:14,700 --> 00:05:16,920 As you can see, I'm in home/student. 112 00:05:16,920 --> 00:05:19,530 And in home/student, though we have different directories 113 00:05:19,530 --> 00:05:21,750 but home/student itself is not there. 114 00:05:21,750 --> 00:05:25,530 So if I want this to be executed in a decent way, 115 00:05:25,530 --> 00:05:30,510 I can move hello.sh to the bin directory 116 00:05:30,510 --> 00:05:32,010 that I just created. 117 00:05:32,010 --> 00:05:34,860 And now, if I use hello, there we can see 118 00:05:34,860 --> 00:05:36,513 that completion is working. 119 00:05:37,953 --> 00:05:40,380 And we can also see ... Oh, command not found. 120 00:05:40,380 --> 00:05:41,640 Why is that? 121 00:05:41,640 --> 00:05:44,880 Because I made a small mistake. 122 00:05:44,880 --> 00:05:49,440 Look, I used echo hello world, and wrote that to hello.sh. 123 00:05:49,440 --> 00:05:52,800 I should have used echo hello world to make 124 00:05:52,800 --> 00:05:55,650 that the contents of my hello.sh. 125 00:05:55,650 --> 00:05:58,434 Well, that's an easy fix, of course. 126 00:05:58,434 --> 00:06:01,880 So let me do a vim on bin/hello.sh 127 00:06:02,760 --> 00:06:05,370 And let me enter an echo here. 128 00:06:05,370 --> 00:06:08,790 And then we have our very simple shell script 129 00:06:08,790 --> 00:06:10,503 that is actually working. 130 00:06:11,580 --> 00:06:15,270 So that is how this PATH variable is used. 131 00:06:15,270 --> 00:06:18,780 Just a quick remark, I used home/student/bin. 132 00:06:18,780 --> 00:06:20,490 home/student/bin is in the PATH. 133 00:06:20,490 --> 00:06:22,290 That's for the local user only. 134 00:06:22,290 --> 00:06:24,450 If I think that my script is so awesome 135 00:06:24,450 --> 00:06:26,520 that everybody should be using it, 136 00:06:26,520 --> 00:06:29,040 I should put it in usr/local/bin. 137 00:06:29,040 --> 00:06:29,873 So, you know what? 138 00:06:29,873 --> 00:06:34,790 Let me do a pseudo mv to move it from a bin/hello.sh 139 00:06:35,760 --> 00:06:38,910 to usr/local/bin. 140 00:06:38,910 --> 00:06:40,080 And there we go. 141 00:06:40,080 --> 00:06:42,480 And at this point, well, for my user account, 142 00:06:42,480 --> 00:06:43,710 nothing is changing. 143 00:06:43,710 --> 00:06:47,280 But now, everybody else can enjoy the benefits 144 00:06:47,280 --> 00:06:48,993 of my wonderful shell script.