OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

What's In A Startup File?

If we take a look inside a typical .bash_profile (taken from a CentOS 6 system), it looks something like this:



# .bash_profile


# Get the aliases and functions if [ -f ~/.bashrc ]; then

. ~/.bashrc

fi

# User specific environment and startup programs PATH=$PATH:$HOME/bin

export PATH

# .bash_profile


# Get the aliases and functions if [ -f ~/.bashrc ]; then

. ~/.bashrc

fi

# User specific environment and startup programs PATH=$PATH:$HOME/bin

export PATH


Lines that begin with a “#” are comments and are not read by the shell. These are there for human readability. The first interesting thing occurs on the fourth line, with the fol- lowing code:



if [ -f ~/.bashrc ]; then

if [ -f ~/.bashrc ]; then

How Is The Environment Established?


. ~/.bashrc

fi

. ~/.bashrc

fi


This is called an if compound command, which we will cover fully when we get to shell scripting in Part 4, but for now, here is a translation:



If the file "~/.bashrc" exists, then read the "~/.bashrc" file.

If the file "~/.bashrc" exists, then read the "~/.bashrc" file.


We can see that this bit of code is how a login shell gets the contents of .bashrc. The next thing in our startup file has to do with the PATH variable.

Ever wonder how the shell knows where to find commands when we enter them on the command line? For example, when we enter ls, the shell does not search the entire com- puter to find /bin/ls (the full pathname of the ls command), rather, it searches a list of directories that are contained in the PATH variable.

The PATH variable is often (but not always, depending on the distribution) set by the

/etc/profile startup file and with this code:


PATH=$PATH:$HOME/bin

PATH=$PATH:$HOME/bin


PATH is modified to add the directory $HOME/bin to the end of the list. This is an ex- ample of parameter expansion, which we touched on in Chapter 7. To demonstrate how this works, try the following:



[me@linuxbox ~]$ foo="This is some "

[me@linuxbox ~]$ echo $foo

This is some

[me@linuxbox ~]$ foo=$foo"text."

[me@linuxbox ~]$ echo $foo

This is some text.

[me@linuxbox ~]$ foo="This is some "

[me@linuxbox ~]$ echo $foo

This is some

[me@linuxbox ~]$ foo=$foo"text."

[me@linuxbox ~]$ echo $foo

This is some text.


Using this technique, we can append text to the end of a variable's contents.

By adding the string $HOME/bin to the end of the PATH variable's contents, the direc- tory $HOME/bin is added to the list of directories searched when a command is entered. This means that when we want to create a directory within our home directory for storing our own private programs, the shell is ready to accommodate us. All we have to do is call


it bin, and we’re ready to go.


image

Note: Many distributions provide this PATH setting by default. Debian based distri- butions, such as Ubuntu, test for the existence of the ~/bin directory at login, and dynamically add it to the PATH variable if the directory is found.


image

Lastly, we have:



export PATH

export PATH


The export command tells the shell to make the contents of PATH available to child processes of this shell.


Top OS Cloud Computing at OnWorks: