Introduction to Bash, VIM & Regex Environment Variables
Transcript from the "Environment Variables" Lesson
>> James: Environment variables are ways that you can kind of both save little pieces of information from the output of your various commands or maybe you wanna just to define constants. And also a way to configure the behavior of some of the system utilities and also the shell itself.
[00:00:21] So the first thing you can try, is just type export. And it should tell you everything that's already defined in your system. So here you can see I've got a lot of these environment variables that, some are defined by Bash like Home, and also Shell which we looked at in the very first part of this workshop.
[00:00:43] Other ones I've set this one called PAGER="less" because some commands they'll check to see if a variable called PAGER, all caps, is defined. And if it is, if they want to display some long bit of output, they'll pipe it into this command. So you can customize what that command is.
>> James: So lots of fun stuff there. If you want to print out what different kind of variable is, I can look at echo $TZ, which I've set on my system. TZ sets the time zone used by the date command, so If I do like America/Los _Angeles. I think with, Angeles.
[00:01:35] Is that how you spell it?
>> Speaker 2: O-s I think.
>> James: Yeah it seems so.
>> Speaker 2: Yeah.
>> James: Then you can see what time it is in LA, or anywhere on Pacific Time. Or I can see like America/ Anchorage to see what time it is fin Alaska right now.
[00:01:52] In the non-illusion part of Alaska anyway. So how this works when you do something like echo like $USER is Bash looks for anything that has a dollar sign in it and has letter and numbers and it will substitute whatever the value of the environment variable is for that.
[00:02:14] So you can use this kind of thing, like if I want to create a file, maybe, I'm going to echo a message to something called $USER.log. Now there's a file called substack.log because that's my user name on this system.
>> James: So, you can also like if I wanna list the home directory, there happens to be an environment variable declared called HOME, all caps.
[00:02:46] Or you could do like /home, on Linux anyways you could do like /home/$USER. I think it's in a different place on Mac, if you have one of those.
>> James: Now, so the next thing that we can do is we can start to declare some of our own variables.
[00:03:06] So, to set a variable we just type its name It's sort of a convention to use all caps, but you can use lowercase as well, it works. So, I'll declare a variable called MSG and I'll say MSG is hi, and now I'll echo out the value of MSG, it's hi.
[00:03:30] I'll also do hello. Now it's hello. So you can also do things like if we go back to the year example with the date command, so if we wanna save the value of the year in an environment variable for later, we can do this with back ticks. So, if we run date+%Y, that's gonna print 2017 and we can save that to a variable.
[00:03:58] So now if we echo out year, see that. So you could also do something like the current year is $YEAR
>> Speaker 2: Is that persistent or is that just as long as long as it's terminal windows are open?
>> James: That variable exists so long as this terminal is open.
[00:04:15] And also it's important to note that if you open a sub shell or another command, it's not going to be defined. So this is a perfect segue into the next bit which is exporting variables. So if you wanna be able to use these environment variables in another program that you're going to spawn.
[00:04:45] You need to export it. So if you've defined environment variable already, you can type export and then its name and now in node I have access to that, or I guess it's called MSG. Now I have access to that variable. You can also declare the contents of that variable in line so if you want to set it to something else.
[00:05:11] Different message, you can do it like that as well.
>> James: So if you want to declare variables, that will kind of be around whenever you open a new terminal, you can edit this file in a new directory. Using Bash it's called .bashrc. There is also one called .profile and that will work on login shows not interactive shows as well but for now if you just open this file .bashrc.
[00:05:45] We'll get into how to use Vim in a moment but if you get stuck, hit Escape or Ctrl+C and then colon, Q. And you might have to do :Q!. So if you get stuck, that's how to get unstuck. But for just a moment, we're gonna go into vi.
[00:06:04] You have to hit I to go into insert mode, which will probably be more familiar. Again, we'll go over all of this stuff in a bit later today. So here, I've already got like a bunch of variables I've exported, like my default time zone I've set up in my Bashrc.
[00:06:21] But I can set a new one when I travel. This is a lot easier than configuring the system, I think. You can also set things like your prefix and your path. So my path has my home directory in it, so I can put things in my tool list /$PREFIX/ bin.
[00:06:39] And anything in there will be a command that I can run. So I can show you what that looks like. All right, so if you hit Ctrl C :q!, you will get out of them no matter what. So I've got all of these little scripts in my prefix bin that aren't.
[00:06:57] I don't know what all those extensive ones are. I must have compiled something and installed it into there. Anyways, that's environment variables. You can also use any environment variable in the arithmetic expressions, which we talked about briefly, so if I have N=5, I can do echo like N*5.
[00:07:23] Inside of an environment, inside of an arithmetic you don't have to put a $ in front of an environment variable. I think the only case that's like that. Probably if you put a $ it will also work, so whatever you like. Okay, so I've kinda been, yeah, question.
>> Speaker 2: I guess,
>> Speaker 2: I don't know how to pronounce his name, but he was just asking about any time of practice example-type stuff that you know of online, aside from the examples that you've put in the slides, to learn some of those basic Bash commands get practice using them.
>> James: Not off the top of my head. Sometimes at the bottom of the main pages, they have some examples but not always. There aren't really that many good resources, unfortunately, for.
>> Speaker 2: Which is why you're doing this course.
>> James: Which is why I'm doing this workshop. [LAUGH] Why I compiled those information, cuz this is basically just, I sat down a while ago and just did a stream of consciousness brain dump about of the tricks that I've learned over the years.
[00:08:35] So I don't know of too many other examples of that, but Maybe there's some. You can-if you know about the command, you can read the man page and you can also search the internet different ways in which people use it. That's about the best I can answer that question, unfortunately.