Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Profile, login, and resource files

From: Unix for Mac OS X Users

Video: Profile, login, and resource files

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.

Profile, login, and resource files

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.

Show transcript

This video is part of

Image for Unix for Mac OS X Users
Unix for Mac OS X Users

82 video lessons · 28697 viewers

Kevin Skoglund
Author

 
Expand all | Collapse all
  1. 3m 57s
    1. Introduction
      1m 14s
    2. Using the exercise files
      2m 43s
  2. 32m 2s
    1. What is Unix?
      7m 27s
    2. The terminal application
      4m 23s
    3. Logging in and using the command prompt
      5m 19s
    4. Command structure
      5m 22s
    5. Kernel and shells
      5m 25s
    6. Unix manual pages
      4m 6s
  3. 15m 58s
    1. The working directory
      2m 49s
    2. Listing files and directories
      3m 59s
    3. Moving around the filesystem
      4m 58s
    4. Filesystem organization
      4m 12s
  4. 1h 4m
    1. Naming files
      5m 41s
    2. Creating files
      2m 19s
    3. Unix text editors
      6m 39s
    4. Reading files
      5m 35s
    5. Reading portions of files
      3m 27s
    6. Creating directories
      2m 40s
    7. Moving and renaming files and directories
      8m 32s
    8. Copying files and directories
      3m 7s
    9. Deleting files and directories
      3m 38s
    10. Finder aliases in Unix
      4m 10s
    11. Hard links
      5m 30s
    12. Symbolic links
      6m 36s
    13. Searching for files and directories
      6m 32s
  5. 34m 58s
    1. Who am I?
      4m 3s
    2. Unix groups
      1m 52s
    3. File and directory ownership
      6m 41s
    4. File and directory permissions
      4m 27s
    5. Setting permissions using alpha notation
      6m 49s
    6. Setting permissions using octal notation
      3m 49s
    7. The root user
      1m 57s
    8. sudo and sudoers
      5m 20s
  6. 52m 34s
    1. Command basics
      4m 4s
    2. The PATH variable
      4m 13s
    3. System information commands
      3m 40s
    4. Disk information commands
      6m 8s
    5. Viewing processes
      5m 0s
    6. Monitoring processes
      3m 36s
    7. Stopping processes
      3m 19s
    8. Text file helpers
      6m 50s
    9. Utility programs
      7m 28s
    10. Using the command history
      8m 16s
  7. 20m 39s
    1. Standard input and standard output
      1m 24s
    2. Directing output to a file
      4m 13s
    3. Appending to a file
      2m 44s
    4. Directing input from a file
      5m 28s
    5. Piping output to input
      4m 40s
    6. Suppressing output
      2m 10s
  8. 41m 28s
    1. Profile, login, and resource files
      9m 11s
    2. Setting command aliases
      6m 59s
    3. Setting and exporting environment variables
      4m 54s
    4. Setting the PATH variable
      6m 10s
    5. Configuring history with variables
      6m 17s
    6. Customizing the command prompt
      6m 5s
    7. Logout file
      1m 52s
  9. 1h 25m
    1. grep: Searching for matching expressions
      5m 21s
    2. grep: Multiple files, other input
      4m 28s
    3. grep: Coloring matched text
      2m 57s
    4. Introduction to regular expressions
      3m 22s
    5. Regular expressions: Basic syntax
      3m 19s
    6. Using regular expressions with grep
      5m 20s
    7. tr: Translating characters
      8m 17s
    8. tr: Deleting and squeezing characters
      5m 30s
    9. sed: Stream editor
      7m 45s
    10. sed: Regular expressions and back-references
      7m 8s
    11. cut: Cutting select text portions
      7m 42s
    12. diff: Comparing files
      4m 35s
    13. diff: Alternative formats
      4m 30s
    14. xargs: Passing argument lists to commands
      7m 25s
    15. xargs: Usage examples
      7m 59s
  10. 42m 25s
    1. Finder integration
      4m 45s
    2. Clipboard integration
      5m 5s
    3. Screen capture
      3m 42s
    4. Shut down, reboot, and sleep
      3m 34s
    5. Text to speech
      2m 36s
    6. Spotlight integration: Searching metadata
      3m 41s
    7. Spotlight integration: Metadata attributes
      4m 24s
    8. Using AppleScript
      5m 23s
    9. System configurations: Viewing and setting
      5m 51s
    10. System configurations: Examples
      3m 24s
  11. 1m 26s
    1. Conclusion
      1m 26s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Unix for Mac OS X Users.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.