1 00:00:00,000 --> 00:00:04,200 [Intro Music] 2 00:00:04,200 --> 00:00:05,600 In this lecture I want to talk 3 00:00:05,670 --> 00:00:07,140 about environment variables. 4 00:00:07,410 --> 00:00:08,820 Environment variable is simply a 5 00:00:08,820 --> 00:00:11,310 variable that is available during 6 00:00:11,310 --> 00:00:13,170 the entire session in the shell. 7 00:00:13,710 --> 00:00:15,180 And you could use environment 8 00:00:15,180 --> 00:00:16,950 variables for example, in shell 9 00:00:16,980 --> 00:00:19,440 scripts. Also environment variables 10 00:00:19,470 --> 00:00:21,870 like PATH are used by system 11 00:00:21,900 --> 00:00:24,750 itself. If you want to get a list 12 00:00:24,780 --> 00:00:26,280 of all environment variables that 13 00:00:26,280 --> 00:00:28,110 were set, you could simply type 14 00:00:28,110 --> 00:00:30,990 command 'env'; three letters; and 15 00:00:30,990 --> 00:00:33,060 you'll see a set of key-value 16 00:00:33,060 --> 00:00:35,730 pairs. Here on the left you see the 17 00:00:35,730 --> 00:00:37,560 name of the variable like TERM, 18 00:00:37,590 --> 00:00:40,410 USER, LC_TERMINAL_VERSION, and so 19 00:00:40,410 --> 00:00:43,470 on. And after equal sign, you see 20 00:00:43,500 --> 00:00:46,410 value for a specific variable. For 21 00:00:46,410 --> 00:00:48,510 example, here you see variable 22 00:00:48,510 --> 00:00:51,480 name, LC_TERMINAL; and its value is 23 00:00:51,540 --> 00:00:54,510 'iTerm2'. Here if you scroll a bit 24 00:00:54,540 --> 00:00:57,197 up, you'll see a variable called 25 00:00:57,197 --> 00:01:00,510 LS_COLORS, and its value is set 26 00:01:00,570 --> 00:01:03,630 here. Also, here you see such 27 00:01:03,630 --> 00:01:06,810 variable as PWD, and it is set to 28 00:01:06,840 --> 00:01:08,970 '/root'. You'll see such 29 00:01:08,970 --> 00:01:11,230 variable as SHELL, and it is set to 30 00:01:11,230 --> 00:01:16,290 '/bin/bash'. Now I'm logged in via SSH to 31 00:01:16,320 --> 00:01:18,870 Docker container as root user. 32 00:01:19,260 --> 00:01:21,990 Let's now try to open up a new SSH 33 00:01:22,020 --> 00:01:24,420 session as 'bogdan' user. Recap 34 00:01:24,420 --> 00:01:25,920 that I have created a new user 35 00:01:25,920 --> 00:01:27,570 before. And if you haven't done 36 00:01:27,570 --> 00:01:29,670 that, please create a new user with 37 00:01:29,700 --> 00:01:32,400 any username. And here let's use 38 00:01:32,400 --> 00:01:34,620 again SSH command, I'll use the 39 00:01:34,620 --> 00:01:38,040 same port 2222. It is exposed port 40 00:01:38,040 --> 00:01:40,350 for Docker container. And here will 41 00:01:40,350 --> 00:01:42,870 be username 'bogdan@localhost', 42 00:01:42,900 --> 00:01:45,690 like this. Let's enter password for 43 00:01:45,720 --> 00:01:48,300 'bogdan' it is '123', in my case; and 44 00:01:48,330 --> 00:01:50,700 I'm in. Let's noe enter 'env' 45 00:01:50,700 --> 00:01:53,340 command here; and now I'll see a 46 00:01:53,370 --> 00:01:56,640 set of similar 'env' variables. Let's 47 00:01:56,640 --> 00:01:59,160 quickly go back to this step where 48 00:01:59,160 --> 00:02:00,930 I have entered same command for 49 00:02:00,930 --> 00:02:03,000 'root' user; and let's compare those 50 00:02:03,030 --> 00:02:05,250 outputs. Here was output for 51 00:02:05,250 --> 00:02:06,960 'root' user; and for example, here you 52 00:02:06,960 --> 00:02:09,240 see environment variable USER, that 53 00:02:09,240 --> 00:02:11,639 is equal to 'root'. But if I switch 54 00:02:11,639 --> 00:02:13,830 to this step, I will see that USER 55 00:02:13,860 --> 00:02:16,560 is set to 'bogdan'. And also here 56 00:02:16,560 --> 00:02:18,900 you see different SSH_CONNECTION 57 00:02:18,900 --> 00:02:21,720 variable. Here, in this case, you 58 00:02:21,720 --> 00:02:25,830 see port 35598. And here in this 59 00:02:25,830 --> 00:02:29,670 case, you see another port, 35578. 60 00:02:30,780 --> 00:02:33,390 Because here are two different SSH 61 00:02:33,420 --> 00:02:35,610 sessions. And actually same 62 00:02:35,610 --> 00:02:37,200 information you see here on this 63 00:02:37,200 --> 00:02:40,950 line, SSH_CLIENT. If I go back to 64 00:02:40,950 --> 00:02:43,290 another tab, where I have logged in 65 00:02:43,320 --> 00:02:45,030 directly to the container, this 66 00:02:45,030 --> 00:02:47,820 step, and enter 'env' variable, I 67 00:02:47,850 --> 00:02:51,480 will not see such variables as I see 68 00:02:51,480 --> 00:02:53,640 here, SSH_CLIENT and SSH_ 69 00:02:53,640 --> 00:02:56,280 CONNECTION; and that proves that 70 00:02:56,310 --> 00:02:58,950 set of environment variables is 71 00:02:58,980 --> 00:03:01,650 created when a specific user logs 72 00:03:01,680 --> 00:03:04,320 into the system. And depending on 73 00:03:04,320 --> 00:03:06,390 the conditions, login conditions, 74 00:03:06,570 --> 00:03:08,400 those sets will be different. 75 00:03:09,630 --> 00:03:11,220 Alright, that is a set of 76 00:03:11,220 --> 00:03:13,410 environment variables. Now question, 77 00:03:13,590 --> 00:03:15,960 how we could access any variable? 78 00:03:16,140 --> 00:03:17,700 It is very simple. We have done 79 00:03:17,700 --> 00:03:19,350 that before. Let's clear terminal 80 00:03:19,350 --> 00:03:22,900 here and simply enter 'echo $PATH', 81 00:03:22,900 --> 00:03:24,800 for example; and here I 82 00:03:24,840 --> 00:03:27,420 get value for PATH variable. 83 00:03:28,710 --> 00:03:30,240 And now let's try to get other 84 00:03:30,240 --> 00:03:34,171 variable, let's say USER, '$USER'; 85 00:03:34,171 --> 00:03:36,000 and I get value for 86 00:03:36,060 --> 00:03:38,280 another variable, USER. It is very 87 00:03:38,280 --> 00:03:40,230 simple. And of course, you could 88 00:03:40,260 --> 00:03:42,870 use variables in shell scripts, for 89 00:03:42,870 --> 00:03:44,700 example, in order to access some 90 00:03:44,730 --> 00:03:47,460 values. And also you could set 91 00:03:47,490 --> 00:03:49,110 additional variables if you want. 92 00:03:49,440 --> 00:03:50,880 For that you need to use command 93 00:03:50,940 --> 00:03:54,240 'export'; and after 'export' will be 94 00:03:54,240 --> 00:03:55,980 name of the new variable or 95 00:03:56,010 --> 00:03:58,170 existing variable; and next comes 96 00:03:58,200 --> 00:03:59,970 equal sign; and value for the 97 00:03:59,970 --> 00:04:01,920 variable. For example, let's create 98 00:04:01,920 --> 00:04:03,015 a new variable called 99 00:04:03,015 --> 00:04:07,740 MY_NEW_VARIABLE, like this; and let's 100 00:04:07,770 --> 00:04:11,040 set it equal to simply 'Test', like 101 00:04:11,040 --> 00:04:13,350 this. Let's press Enter. And now 102 00:04:13,350 --> 00:04:15,600 let's list all the variables. And 103 00:04:15,600 --> 00:04:17,160 actually if you want, you could use 104 00:04:17,160 --> 00:04:20,820 here, 'grep', and here will be 'MY_', 105 00:04:21,060 --> 00:04:23,339 let's say 'NE', like this; and I'll 106 00:04:23,339 --> 00:04:26,820 get value for MY_NEW_VARIABLE. It 107 00:04:26,820 --> 00:04:28,500 is actually available also in this 108 00:04:28,530 --> 00:04:30,900 output. Let me scroll a bit up and 109 00:04:30,900 --> 00:04:32,516 yes, here is this variable, 110 00:04:32,516 --> 00:04:36,090 MY_NEW_VARIABLE, and its value is 'Test'. And 111 00:04:36,090 --> 00:04:37,500 of course now you could easily 112 00:04:37,500 --> 00:04:39,570 access this new variable, let 113 00:04:39,570 --> 00:04:42,450 me clear terminal, 'echo $ 114 00:04:42,540 --> 00:04:44,670 MY_NEW_VARIABLE'; and here's its 115 00:04:44,670 --> 00:04:48,150 value. That is that simple. Also, 116 00:04:48,180 --> 00:04:50,130 when you run some applications, 117 00:04:50,130 --> 00:04:52,110 like for example, Node.js 118 00:04:52,110 --> 00:04:54,450 Python, Java applications, many 119 00:04:54,450 --> 00:04:56,670 applications require runtime 120 00:04:56,700 --> 00:04:59,280 variables. And usually you could 121 00:04:59,310 --> 00:05:01,170 set such environment variables 122 00:05:01,230 --> 00:05:03,180 before start of any application 123 00:05:03,330 --> 00:05:06,270 using similar approach. Alright, 124 00:05:06,450 --> 00:05:07,950 that's all about environment 125 00:05:07,950 --> 00:05:10,900 variables, and now you'll get small challenge. 126 00:05:10,900 --> 00:05:14,800 [Background Music] 127 00:05:14,800 --> 00:05:16,800 You need to create a new 128 00:05:16,830 --> 00:05:19,189 variable called MY_NAME, 129 00:05:19,189 --> 00:05:22,410 MY_NAME. Afterwards, you 130 00:05:22,410 --> 00:05:24,780 need to create a new shell script 131 00:05:25,110 --> 00:05:29,490 called name.sh. And inside of this 132 00:05:29,520 --> 00:05:31,740 new script, you need to print your 133 00:05:31,740 --> 00:05:33,690 name to the terminal using 'echo' 134 00:05:33,690 --> 00:05:35,700 command. And afterwards, of course, 135 00:05:35,730 --> 00:05:37,530 you need to execute this script. I 136 00:05:37,530 --> 00:05:39,000 wish you good luck guys, and after 137 00:05:39,000 --> 00:05:40,410 the pause, I'll show you solution 138 00:05:40,410 --> 00:05:42,477 for this challenge. I'll see you next. Bye. 139 00:05:42,477 --> 00:05:44,106 [no audio]