3

Examining the contents of ~/.bash_profile shows:

codio@data-burma:~$ cat ~/.bash_profile
if [ -f ~/.bashrc ]; then
  . ~/.bashrc
fi

cd /home/codio/workspace
codio@data-burma:~$

What does all this code in .bash_profile mean?

closed as unclear what you're asking by TheWanderer, Jacob Vlijm, Eliah Kagan, Eric Carvalho, waltinator Feb 5 '17 at 20:31

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Are you using Ubuntu? – Zanna Feb 5 '17 at 15:03
  • I'm using a program that emulates Ubuntu, as this is only for my GCSEs – Slayde098 Feb 5 '17 at 15:09
  • 1
    As Zanna and Yaron have suggested, it's unexpected to have a .bash_profile file in Ubuntu that looks like this (most often, one doesn't have .bash_profile at all, just .profile). So please edit this to tell what Ubuntu release you are using, the output of lsb_release -a and cat /etc/issue, and an explanation of how you installed or upgraded Ubuntu. Did you copy your home directory from another machine? Did that machine run Ubuntu? – Eliah Kagan Feb 5 '17 at 15:10
  • 1
    @George you can check /etc/skel to see which shell initialization files Ubuntu has by default. .bash_profile definitely isn't in any default Ubuntu installation. – muru Feb 5 '17 at 16:12
  • 3
    @George no idea, only you (and probably your shell history) can say. – muru Feb 5 '17 at 16:19
5

The below answer is based on .bash_profile vs. .bashrc by Josh Staiger. For more details please review the content of that page.

According to the bash man page, .bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells.

Most of the time you don’t want to maintain two separate config files for login and non-login shells — when you set a PATH, you want it to apply to both. You can fix this by sourcing .bashrc from your .bash_profile file, then putting PATH and common settings in .bashrc.

To do this, add the following lines to .bash_profile:

if [ -f ~/.bashrc ]; then
   source ~/.bashrc
fi

The above code does the following:

  1. Check if ~/.bashrc exists.
  2. If the file exists it, source it ("execute it").
-1

The meaning of the default code in the ~/.profile file

The first section:

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi

It's verifying that your shell is bash. The BASH_VERSION variable is only set when running bash so this is an easy way of checking. Then it will source the ~/.bashrc file if it exist. The .bashrc file does exist on Ubuntu installations. So it will be sourced. It's this .bashrc file that you will put your custom configurations such as special variable settings and aliases.

For instance, if you want to type cls to clear your screen, you could alias it with:

$ alias cls="clear"

After that command, this would clear your screen:

$ cls

Just add that line to your .bashrc file, then the next time you open a terminal you could clear the screen with either cls or clear.

The second section:

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

This block is checking if you have created a ~/bin folder in your home folder. If you have it will add this folder to your execute path. This way any custom scripts or special commands that you want to type in without using a full path, you could place it there (or a link to it from there) be able to execute the command without typing it the full path.

Summary

The !/.profile is a script that is run when you log in or start a terminal. Any commands are settings can be added to the script. But by default it (1) checks for a ~/.bashrc file and loads (source) it and (2) checks for a ~/bin folder and adds it to the default PATH if it exists.

Update:

I described above the meaning of the content of the .profile as provided by Ubuntu by default. Some systems will have different content.

You have a line in your .profile that is different from the default. As explained it's a script and is run when you log into the system (or run a new terminal.).

Your line that reads:

cd /home/codio/workspace

Changes the current directory to the /home/codio/workspace. This is done at times when a user has a linked home directory and the the prompt shows the full paths of the home directory instead ~$ as the home path. The other lines have been explained. Hope this clarifies the custom line added to your the particular .profile script included in your question.

  • 1
    Indeed, but it seems like you didn't read the question body? – Zanna Feb 5 '17 at 15:41
  • @Zanna I explained the meaning of the content of the .profile for how it's used in Ubuntu. I also explained how to add other commands to do things. It appears the OP has a line cd /home/codio/workspace added to his .profile file. It performs the way I explained in the original answer, that you can add other commandlines that will be processed. If there's something else that you think should be clarified in the answer or I'm missing what you are currently pointing out, thanks in advance for calling it to my attention. – L. D. James Feb 5 '17 at 15:53
  • 1
    Well, technically the question is about ~/.bash_profile, which a) doesn't exist by default in Ubuntu, and b) supersedes ~/.profile if created. – muru Feb 5 '17 at 16:03
  • @muru Thanks. Now I see. I was reading the title, and checking my 16.04 installation and explaining the default superseded version. Looking at one of my older machines I see that ~/.bash_profile resembles closely the ~/.bashrc. It wasn't immediately clear that he wanted specific lines explained. I thought by the title, he was trying to understand the purpose of the profile file itself. So I explained the purpose and the meaning of the lines that are in the current version. Hopefully I'm closer on point, by having included the specific lines question. – L. D. James Feb 5 '17 at 16:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.