1 00:00:00,000 --> 00:00:01,090 [No audio] 2 00:00:01,090 --> 00:00:04,422 Environment variables. What are 3 00:00:04,496 --> 00:00:06,270 the environment variables? 4 00:00:07,490 --> 00:00:11,718 A regular definition or off the book definition of 5 00:00:11,744 --> 00:00:14,930 environment variable is, an environment variable is a 6 00:00:14,930 --> 00:00:20,262 dynamic-named value that can affect the way running 7 00:00:20,336 --> 00:00:22,326 process will behave on a computer. 8 00:00:22,508 --> 00:00:24,406 They are part of the environment 9 00:00:24,478 --> 00:00:26,550 in which a process runs. 10 00:00:27,350 --> 00:00:32,478 So in simple words, environment variables are a set 11 00:00:32,503 --> 00:00:37,078 of defined rules and values to build an environment. 12 00:00:37,174 --> 00:00:40,520 So to make it more simpler I will 13 00:00:41,030 --> 00:00:44,274 give you an example of a house. 14 00:00:44,432 --> 00:00:47,918 We have our house, by the way, it's just a tall 15 00:00:47,954 --> 00:00:51,420 house that's the only house that I could pick out from 16 00:00:51,990 --> 00:00:55,174 one of the search engine that would depict the dining room 17 00:00:55,212 --> 00:00:58,558 and bedroom, anyway, so we have our house, just take an 18 00:00:58,584 --> 00:01:01,306 example of our own house, the house that you live in. 19 00:01:01,488 --> 00:01:04,569 That house has its own environment. 20 00:01:04,950 --> 00:01:10,690 What it means is, it has a bedroom where you sleep, 21 00:01:11,370 --> 00:01:16,378 it has a dining room where you eat, sorry it has 22 00:01:16,404 --> 00:01:18,874 a kitchen where you cook, and the dining room where you 23 00:01:18,912 --> 00:01:23,146 eat, and it has a playroom where you play, and 24 00:01:23,208 --> 00:01:25,886 so many different rooms for other purposes. 25 00:01:26,018 --> 00:01:30,634 Now those are defined a set of rules or someone would 26 00:01:30,672 --> 00:01:35,294 say an unwritten rules. You cannot really sleep in the kitchen, 27 00:01:35,342 --> 00:01:37,786 or you cannot really cook in the bedroom, right? 28 00:01:37,908 --> 00:01:41,950 Because those places are built for that specific 29 00:01:42,060 --> 00:01:47,040 needs, so that's the environment of your house. 30 00:01:47,430 --> 00:01:52,598 Similarly, in Linux, we have environment. Environment meaning 31 00:01:52,634 --> 00:01:54,958 when a user logs in the user is 32 00:01:54,984 --> 00:01:57,550 given an environment by saying, hey, 33 00:01:57,660 --> 00:02:02,362 your home directory is this, your process or your shell where you're going 34 00:02:02,376 --> 00:02:06,334 to run the scripts those paths are these, these are the 35 00:02:06,372 --> 00:02:10,234 coloring you will get for all directory and listing, and 36 00:02:10,270 --> 00:02:12,538 these are a few set of rules that you will 37 00:02:12,564 --> 00:02:16,994 get so they are inherited automatically through the global environment. 38 00:02:17,162 --> 00:02:20,434 Those environments by the user can also be 39 00:02:20,472 --> 00:02:23,580 changed by a specific user as well. 40 00:02:24,270 --> 00:02:29,390 So to view all environment variables in Linux environment 41 00:02:29,450 --> 00:02:32,710 you have to run the command printenv 42 00:02:32,880 --> 00:02:36,650 sorry by the way it's not evn, it's env. 43 00:02:36,710 --> 00:02:41,518 I should fix that and I will definitely will, or 44 00:02:41,664 --> 00:02:45,742 you could just simply type env. So both commands are 45 00:02:45,756 --> 00:02:47,990 the same, we'll give you the same results, 46 00:02:48,110 --> 00:02:51,490 if you wanted to list what is in your environment. 47 00:02:52,230 --> 00:02:56,122 To view one specific environment variable, you could run a 48 00:02:56,136 --> 00:03:01,606 command like echo shell, so $SHELL, if you wanted 49 00:03:01,668 --> 00:03:05,894 to find out what is your shell. Usually the shell 50 00:03:05,942 --> 00:03:11,194 in Linux is given as bash, bin bash, that's a 51 00:03:11,232 --> 00:03:14,398 bash shell, corn shell, korn shell, whichever your shell is. 52 00:03:14,484 --> 00:03:15,994 If you want to find out your 53 00:03:16,032 --> 00:03:19,634 shell, you will specifically do echo $SHELL. 54 00:03:19,742 --> 00:03:21,850 If you want to find out specifically 55 00:03:22,230 --> 00:03:25,874 your path, you'll do echo $PATH. 56 00:03:25,982 --> 00:03:27,382 Anyway, I'll show you in a 57 00:03:27,396 --> 00:03:29,314 few minutes what I'm talking about. 58 00:03:29,472 --> 00:03:32,578 To set the environment variables on the fly for 59 00:03:32,604 --> 00:03:37,198 example you could do export TEST equal one and 60 00:03:37,224 --> 00:03:39,878 then do echo $TEST, then when you type echo 61 00:03:39,914 --> 00:03:42,600 $TEST you will get a result as one. 62 00:03:43,410 --> 00:03:47,194 If you want to set the environment variable permanently then 63 00:03:47,232 --> 00:03:52,426 you will have to modify that .bashrc file, and 64 00:03:52,488 --> 00:03:59,045 there you will have to put in TEST equals single quote, 123, single quote. 65 00:03:59,045 --> 00:04:01,536 Why 123, why not 1? It's just numbers, 66 00:04:01,536 --> 00:04:03,178 I should have picked 1 anywhere to be 67 00:04:03,204 --> 00:04:05,878 consistent, and then you type export TEST. 68 00:04:06,024 --> 00:04:09,778 But if you wanted to set the environment globally and 69 00:04:09,804 --> 00:04:12,266 you want to have it permanently you have to modify 70 00:04:12,338 --> 00:04:17,786 the profile and bashrc that are inside of etc. 71 00:04:17,978 --> 00:04:21,274 Now let me log in to my Linux machine and 72 00:04:21,312 --> 00:04:25,642 I will show you exactly what the shells are. 73 00:04:25,836 --> 00:04:29,326 So I am logged in and I'm logged in as myself, 74 00:04:29,448 --> 00:04:33,782 iafzal, and if I wanted to list the environment 75 00:04:33,866 --> 00:04:39,002 I will type printenv, hit enter, and as you see it's 76 00:04:39,086 --> 00:04:43,246 showing me a lot of stuff, and it's going straight up. 77 00:04:43,308 --> 00:04:48,826 I could do printenv, and pipe it to more 78 00:04:48,888 --> 00:04:51,300 so I could view it one page at a time. 79 00:04:52,110 --> 00:04:55,010 And you will see it will tell you my terminal, 80 00:04:55,070 --> 00:04:58,354 meaning the terminal that will be used to log in, 81 00:04:58,392 --> 00:05:00,086 it would be like an xterm. 82 00:05:00,218 --> 00:05:03,446 My hostname is myfirstlinuxvm. 83 00:05:03,578 --> 00:05:05,038 This is my SESSION ID, 84 00:05:05,124 --> 00:05:07,202 which will automatically be assigned. 85 00:05:07,346 --> 00:05:08,798 This is my SHELL. 86 00:05:08,954 --> 00:05:11,662 Whenever I log in a shell is given to me and 87 00:05:11,676 --> 00:05:15,986 that shell is /bin/bash. The history size, which is whenever 88 00:05:16,058 --> 00:05:20,674 I run certain commands those commands will be kept in history 89 00:05:20,772 --> 00:05:22,942 when you type history command. By the 90 00:05:22,956 --> 00:05:24,418 way, history command is something that I 91 00:05:24,444 --> 00:05:26,162 have covered in the later lectures. 92 00:05:26,306 --> 00:05:29,674 So history command, that is telling you it 93 00:05:29,712 --> 00:05:32,062 has to record up to 1000 line. 94 00:05:32,196 --> 00:05:33,818 So this is the environment. 95 00:05:33,974 --> 00:05:38,566 What is the SSH_TTY terminal that will be given? 96 00:05:38,688 --> 00:05:39,794 What is the USER, 97 00:05:39,842 --> 00:05:41,858 what comes in of course is my user. 98 00:05:41,954 --> 00:05:43,562 What are the different colors? 99 00:05:43,646 --> 00:05:47,590 You see LS_COLORS. When you run ls -l, 100 00:05:47,640 --> 00:05:50,326 you see a different color for directory, different color for 101 00:05:50,388 --> 00:05:53,400 file, those are the colors you could change. 102 00:05:53,790 --> 00:05:57,334 Like every time you put in a gif file, it will 103 00:05:57,372 --> 00:06:02,518 show up in the color code 01;35. What it 104 00:06:02,544 --> 00:06:04,498 refers to, I'm not sure, you can google it, 105 00:06:04,524 --> 00:06:06,814 you could find out. Then going 106 00:06:06,852 --> 00:06:09,190 forward where my MAIL will go. 107 00:06:09,240 --> 00:06:11,674 Whenever someone sends me a mail, where it will 108 00:06:11,712 --> 00:06:15,530 go, it will go to /var/spool/mail/iafzal. 109 00:06:15,590 --> 00:06:18,230 So these are the set rules, set-defined rules. 110 00:06:18,290 --> 00:06:19,370 Where is my PATH? 111 00:06:19,430 --> 00:06:23,218 Every time I run a command, the operating system will 112 00:06:23,244 --> 00:06:28,030 go in and find the location of that command, and 113 00:06:28,080 --> 00:06:31,630 if that location is not listed in one of these 114 00:06:31,680 --> 00:06:34,550 path, it will not find that command. 115 00:06:34,730 --> 00:06:38,804 My own bin directory will be /home/iafzal/bin. 116 00:06:38,804 --> 00:06:40,658 My home directory, 117 00:06:40,694 --> 00:06:44,014 if I run pwd, would be /home/iafzal. 118 00:06:44,014 --> 00:06:47,401 The LANG that is set to UTF-8. 119 00:06:47,401 --> 00:06:53,110 Some HISTCONTROL, my HOME directory, my LOGNAME, 120 00:06:53,160 --> 00:06:56,770 my DATA path, SSH_CONNECTION, and a couple of other 121 00:06:56,820 --> 00:06:59,662 things which I could go over in detail, but I 122 00:06:59,676 --> 00:07:02,098 don't want to take up a lot of your time. 123 00:07:02,244 --> 00:07:05,258 So these are the set variables, 124 00:07:05,294 --> 00:07:07,500 set environment that is given to you. 125 00:07:09,270 --> 00:07:13,378 If you want to check which environment variable, a 126 00:07:13,404 --> 00:07:15,658 specific environment that you wanted to check, you will 127 00:07:15,684 --> 00:07:20,410 do echo and you will do for example, $MAIL. 128 00:07:21,030 --> 00:07:23,662 You always have to start with dollar sign by 129 00:07:23,676 --> 00:07:26,278 the way, because this way it will understand oh, 130 00:07:26,304 --> 00:07:28,198 this is a variable that I'm talking about. 131 00:07:28,284 --> 00:07:31,162 So all I'm trying to find out what is 132 00:07:31,176 --> 00:07:34,118 the path that is defined for my mail. 133 00:07:34,274 --> 00:07:36,482 Right here you see it says MAIL 134 00:07:36,566 --> 00:07:38,990 equals /var/spool/mail/iafzal. 135 00:07:39,110 --> 00:07:42,958 So if my shell is reading the correct 136 00:07:43,044 --> 00:07:46,618 environment variable, I should get this back. 137 00:07:46,764 --> 00:07:52,342 So now if I do echo $MAIL, /var/spool/mail/iafzal. Perfect. 138 00:07:52,416 --> 00:07:53,878 Yes, I am getting that. 139 00:07:53,964 --> 00:07:56,398 Now if I want to do path, I 140 00:07:56,424 --> 00:07:59,974 will do echo $PATH, and it will 141 00:08:00,012 --> 00:08:05,630 list every directory where my path has defined. 142 00:08:05,690 --> 00:08:09,180 Just like that, the path is defined here. 143 00:08:10,110 --> 00:08:17,748 If I wanted to do my home, I will do echo $HOME, 144 00:08:17,850 --> 00:08:21,970 and you will see this is my home directory, right here HOME. 145 00:08:22,080 --> 00:08:23,846 This is my home directory. 146 00:08:24,038 --> 00:08:27,454 Also, sometimes what people do if they are in 147 00:08:27,492 --> 00:08:31,179 one directory, let's say if you are in at etc directory, 148 00:08:31,179 --> 00:08:33,552 you do pwd, you could simply do 149 00:08:33,552 --> 00:08:39,299 cd $HOME, and it will take you home. 150 00:08:39,929 --> 00:08:43,320 That's how your environment is defined as well. 151 00:08:43,770 --> 00:08:47,690 So if you want to set an environment 152 00:08:47,750 --> 00:08:51,361 variable while you're logged into the system. Let's 153 00:08:51,385 --> 00:08:54,178 say you wanted to do TEST equal 1. 154 00:08:54,264 --> 00:08:57,720 So you would type export TEST, 155 00:08:58,050 --> 00:09:00,120 which is variable equal one. 156 00:09:00,870 --> 00:09:05,158 And then when you do echo $TEST, OR 157 00:09:05,184 --> 00:09:09,094 $TEST, you will see the value one. 158 00:09:09,192 --> 00:09:11,990 But of course this is not permanently defined. 159 00:09:12,110 --> 00:09:14,614 If you log out of your system right 160 00:09:14,652 --> 00:09:17,518 now, this environment variable will go away. 161 00:09:17,664 --> 00:09:19,958 So if you want to set this environment 162 00:09:20,054 --> 00:09:25,102 variable permanently in your operating system, then you 163 00:09:25,116 --> 00:09:29,570 would have to actually, vi your bashrc file. 164 00:09:29,630 --> 00:09:31,034 So you are in your home directory, 165 00:09:31,082 --> 00:09:35,654 first make sure, then I will recommend that you copy 166 00:09:35,822 --> 00:09:41,009 your bashrc file, which is .bashrc 167 00:09:41,009 --> 00:09:48,217 and make it bashrc.orig. 168 00:09:48,366 --> 00:09:49,974 So this way if you make a mistake, 169 00:09:50,022 --> 00:09:52,754 you can always revert back to this. 170 00:09:52,852 --> 00:09:58,960 Now you vi your bashrc, and hit enter. 171 00:10:00,970 --> 00:10:05,166 Now come all the way down and define your variable. 172 00:10:05,238 --> 00:10:09,798 So I wanted to define my variable as TEST equal, 173 00:10:09,954 --> 00:10:12,470 now this time I just want to do 123. 174 00:10:12,580 --> 00:10:14,762 When you define a variable inside of the 175 00:10:14,776 --> 00:10:18,314 bashrc, you put a quote, single quote. 176 00:10:18,472 --> 00:10:23,320 Then you hit enter, and then type export TEST. 177 00:10:24,190 --> 00:10:26,510 Go ahead and save your file. 178 00:10:27,550 --> 00:10:30,760 Now if you do test again, 179 00:10:32,110 --> 00:10:36,218 echo $TEST, you'll get one. 180 00:10:36,304 --> 00:10:37,310 Why are you getting one? 181 00:10:37,360 --> 00:10:42,878 Because it is still reading that export TEST equal one 182 00:10:42,964 --> 00:10:45,662 and then it is giving you that same value. 183 00:10:45,736 --> 00:10:48,662 You have to log out and log back in. 184 00:10:48,796 --> 00:10:51,446 So for me I could just go here 185 00:10:51,508 --> 00:10:54,194 and click on this little icon up here 186 00:10:54,232 --> 00:10:56,870 and type in Duplicate Session from my PuTTY. 187 00:10:57,700 --> 00:11:02,846 I will do iafzal, login, hit enter, 188 00:11:03,028 --> 00:11:09,734 and now when I do echo TEST, you will 189 00:11:09,772 --> 00:11:12,722 see it is giving me 123. 190 00:11:12,916 --> 00:11:18,210 Because this session I have a new session, 191 00:11:18,270 --> 00:11:21,338 and when I logged into a new session, every time I 192 00:11:21,364 --> 00:11:28,850 log into a new session, my login will read .bashrc file, 193 00:11:29,710 --> 00:11:36,938 this file, and when you do ls -l, and by the way 194 00:11:36,964 --> 00:11:39,974 should go up, you will see .bashrc is not 195 00:11:40,012 --> 00:11:43,170 showing up, because it is a hidden file. 196 00:11:43,230 --> 00:11:46,058 Anytime you need to view a hidden file, you 197 00:11:46,084 --> 00:11:50,680 have to do ls -la, hit enter. 198 00:11:51,010 --> 00:11:54,782 And now you could go up, and you 199 00:11:54,796 --> 00:11:58,742 will see your .bashrc file right here. 200 00:11:58,936 --> 00:12:00,726 This is the one I modified. 201 00:12:00,918 --> 00:12:03,998 If you have modified, you could modify this file to 202 00:12:04,024 --> 00:12:06,914 set your environment and then make sure you log out 203 00:12:06,952 --> 00:12:09,630 and log back in or start a new session, 204 00:12:09,750 --> 00:12:11,198 if you want to have your 205 00:12:11,224 --> 00:12:14,080 environment variable take effect right away. 206 00:12:14,470 --> 00:12:17,562 And the last one I have right here, I'll 207 00:12:17,586 --> 00:12:20,630 go ahead and close the session behind it, is 208 00:12:20,680 --> 00:12:23,442 if I want to set globally environment permanently. 209 00:12:23,526 --> 00:12:26,474 Meaning if I wanted to set these environment not 210 00:12:26,512 --> 00:12:29,222 just for myself, I wanted to set it for 211 00:12:29,296 --> 00:12:32,380 everyone else who logs into that system, 212 00:12:33,550 --> 00:12:35,834 make sure you know what you're doing first of all. 213 00:12:35,872 --> 00:12:38,498 So I will not recommend you that you 214 00:12:38,524 --> 00:12:40,466 do it right now in your lab. 215 00:12:40,588 --> 00:12:42,954 However, I will explain you the files 216 00:12:43,002 --> 00:12:44,730 that you would need to modify. 217 00:12:44,910 --> 00:12:49,850 Those files are at /etc/profile. 218 00:12:50,650 --> 00:12:52,418 This is the file that you would have to 219 00:12:52,444 --> 00:12:59,175 modify or you have to modify /etc/bashrc. 220 00:12:59,830 --> 00:13:02,870 So you can modify these files and you will put in 221 00:13:02,920 --> 00:13:07,902 exactly the same way you put it in your .bashrc. 222 00:13:08,046 --> 00:13:10,770 But again, as I said, please do not modify 223 00:13:10,830 --> 00:13:12,998 this because this is lab environment, I don't want you 224 00:13:13,024 --> 00:13:16,790 to modify globally, and it will cause problem. 225 00:13:16,900 --> 00:13:18,954 And then you will know the root cannot 226 00:13:19,002 --> 00:13:21,520 log in or yourself cannot log in. 227 00:13:21,910 --> 00:13:23,678 But if you want to do it for your 228 00:13:23,704 --> 00:13:27,746 production environment or your corporate environment where you're working, 229 00:13:27,808 --> 00:13:30,246 I would recommend you to do it in development 230 00:13:30,318 --> 00:13:34,856 first and then you try it in production. All right, good luck. 231 00:13:34,856 --> 00:13:35,830 [No audio]