Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In this chapter, we're going to take a look at how we can configure our working environment in Unix, or put another way, how can you customize bash to your personal preferences? Well, the way we'll do that is providing commands inside bash resource files. Let's take a look at those files and understand how bash works with them. First, let's talk about the initialization that bash goes through whenever we first log in. Now on a Mac, logging into bash is the same thing as opening up a Terminal window. You open up a new Terminal window and it logs us into the bash shell, right? It auto logs you in as the user that you are in the Finder.
Well, when it does that, the first thing that bash does upon login is it reads any commands that are inside the directory etc inside the file profile. Those are master default commands that every bash user will get. We don't want to mess with those. Leave those alone. Those are default settings. Let Apple take care of managing those for you, all right. That's the defaults all users get. Instead, we want to take care of just personal customizations that apply only to us. So because these are our personal customizations, they are going to live inside our user directory. So notice that I've got four file paths there.
Each one begins with that tilde indicate that it's in the user directory. Also notice that each of them begins with a period. These are dot files. They are not visible in the Finder and the dot is to indicate that it's really a configuration file. So we have four choices. We have .bash_profile, .bash_login, .profile, and .login, and what bash does is after finishes reading that main profile, it goes to our user directory and it sees which one of these four files it can find in the order that I have them listed. When it finds one, it reads the commands that are in there and then it ignores the rest of those.
Okay, so it just goes finds one and only executes one file, so there is no point in having both .bash_profile and .bash_login. Any commands that are in .bash _login will just get ignored. So I'm going to recommend that we put everything into .bash_profile. .bash_profile and .bash_login are really specific for bash. .profile and .login are there for historical and backwards compatibility reasons and they also would potentially apply to working in other shells as well. But the things that we put in these configuration files are often very shell specific.
So the things we put in there, the syntax that we use, might only apply to bash. So if we use other shells, we're really better off using their profiles instead. So let's put everything inside .bash_profile. Now that only covers when we login to a new shell by opening a new Terminal window. If from the command line we type the word bash, it opens up a new bash shell, but it's not a login shell. There is a distinction between that. The login shell is the first time that we come to it when we open up that Terminal window. Typing bash on the command line opens up a sub-shell, and whenever we start a new bash sub-shell then it executes the commands that are inside .bashrc.
RC is to indicate resource. So here is the thing. Anything that we put in .bash_profile doesn't become available to us in the sub-shell. Anything we put into .bashrc isn't available to us until we go into a sub-shell. So we're going to want to have a way so that we can have one set of commands that are available to us in both cases, right, because we want the same configuration regardless of whether in this master shell or whether inside a sub-shell. Now you may not be in a sub-shell very often, but it's still going to be a good idea to set it up the way that I'm going to show you.
Before we do that, let me also just mention that there is one last file that is the logout file. Whenever we log out, it will run any commands that are inside .bash_logout. Okay, so those are going to be the logout files. If we want to perform specific actions as we're logging out, we can do them here. Now as I said, this is the load order for the bash shell, which is what we're working in. If you want to work in other shells, you can go to Wikipedia. They have a nice explanation of the load order for all those different shells. So you'll be able to figure out exactly where you have to put your configurations for those shells.
Okay, so now how do we handle this problem between the login shell and the sub-shell? Well, it's a really easy solution. What we'll have to do is just add a bit of code to our .bash_profile that says if you can find this .bashrc file, well, then load everything that's in it. This is really the only bash scripting that we're going to do. But I'm going to just give it to you, Say if the file exists,--that's what's inside those square brackets is a shortcut for whether the file exists or not. Then read in the .bashrc file. That's it. That source is just saying all right. Let's execute whatever is in there.
So we'll put this in our .bash_profile and now all of our commands and all our configuration can go in .bashrc. Whenever we log in, it will run that .bashrc file. Whenever we switch to a new shell, it will run that .bashrc file. Okay, it's a very simple solution that allows us to have the same configuration applied to both cases. Let's try it out. So here I am in my main user directory. Make sure you're in the same place. ls-la. You'll see that I have my bash history file, but I don't have any of those other configuration files that we just talked about.
So the first thing let's do, let's create our .bashrc file. All right! We can just do that with touch.bashrc. Notice that there is no underscore and its just bashrc. Now we have that file. It is empty, but the file exists. Now let's create our .bash_profile. nano .bash_profile. There it is and inside nano what we're going to do is just paste in that code that I just gave you. So pause the movie if you want to copy it down. Make sure that you get the punctuation and everything correct. Notice also that this if statement ends with fi.
That's how we close an if statement, right? So it starts with if and it ends with fi. So make sure that you got that right. That's not a mistake. Then when you're done, we're going to save that file and we'll save it to .bash_profile. Now we can use that trick that we learned in the history section just to take a look at the file, see what's in there, and we can also do the same thing for .bashrc. You'll see that there is nothing inside that file. All right! But both of them exist. Now we see both of them there. Okay, so we're set up now. Now we've set things up so that the .bashrc file is our master file.
Now that doesn't mean we can't still put things in the .bash_profile. Let's try this. .bash_profile and before I load in this .bashrc file, I'm just going to up above it paste in some more commands. All right! TO HERE Now you can put whatever you want here. These are all things we've covered before. I've got echo in a new line. then echo without making a new line. Welcome to Unix on Mac OS X. and then a semicolon for a new command, whoami, so that I put my name. and then another new line.
and then another blank line followed by Today is the date and with the date, I've provided a format that's just string from Time formats, you can look those up if you need reference for what all the symbols mean, we'll see it in just a second. And then I've got an echo. and then I'm using Cal to display a calendar that will show the current month. and then Uptime. All right! So, all basic things that we've learned before, Ctrl+X, and yes, all right, there we are. Now let's quit this window and let's open up a new one, all right? So I'm going to just close this window. I'll click up here in the red dot, and now Command +N will open up a New window. Look what it did.
It came up and gave me all those commands upon login. If I type bash, I don't get them. All right! I am in a new shell, now but I'm not in my login shell. They are only executed one time. Let's just see the difference. Let's go into .bashrc and let's put in just the last part, that Uptime, right? So whenever we login to a new shell, we'll just report the Uptime to us followed by a blank line and then keep going with whatever it was doing. All right! So let's exit out of that. Let's now go bash. All right! There it is.
Now executes what's in that .bashrc file. Let's go back and exit out of this again. Let's close this window. Let's open up a new one now. It says yes, we do want to close it, and here we go. Notice now it gives me the Uptime twice. We know why that is. We do nano .bash_profile. Now you can see that it's doing the Uptime here and then it's doing it again when it reads in the file here. Okay, so that should help you understand how these two things are working. Incidentally, in these configuration files a Pound sign indicates a comment. All right! So that comments it out.
I can make all sort of comments here, and I can say for example, at the top of this, I put a comment that says, This only runs on user login, and then I can say, I'll just actually delete these lines. Let's go ahead and just remove those and say, This loads in the configuration in .bashrc, put all configuration there.
So that's how we can do comments by just using those Pound signs or hash. So let's Ctrl+X out of there, and now we can save that file. Once again, we'll close that window and open a new one and now you can see only reports Uptime to us. One time, I type bash, we get a new shell. It just does the Uptime for us again. Okay, now you can put whatever you want in there. I'm not trying to tell you have to it exactly my way. I just want to give you some ideas on what's possible, and mostly to show you which files to put your things in and the sort of interplay that happens between those two files.
Now that we understand where we should locate our changes, let's look at what other configurations are possible.
Get unlimited access to all courses for just $25/month.Become a member
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.