N&Bx: dissecting bash, 2: environment

Hi, and this is the second blog in the series Nuts&Bolts with Linux, or ‘N&Bx’.

I use bash daily at home and work, and thought of writing a bit of introduction to this “swiss army knife of CLI”. Bash comes in the line of Unix shells, and it has some of the principles and roots in the

Bash and its environment

Bash, the fine shell that is readily available for a wide range of operating systems, was introduced in the first part.

We’ll go further into the dungeons, and focus on how a bash shell session gets its environment set up.

The system-wide file for all bash instances is:


AND then additionally

~/.bash_profile (if this exists), OR
~/.bash_login (is this exists), OR finally

bash reads and executes commands from the first such config file (of the above 3) that exists and is readable (user has +r right over the file).

Last setting “rules” (over previous ones)

Sometimes the various profile files might have settings that affect the same variable. In that case, the last of those settings will prevail. Unless the profile’s setting is written such that it takes previous settings into account; typical example is PATH variable setting with:

export PATH="$PATH:/usr/newpath"

“The new PATH shall be the current PATH, appended with string :/usr/newpath”

Is the ‘export’ necessary?

In a word, yes. Without an export, the assignment would only be scoped within the execution of the profile script – not your shell session. You can easily try out the difference.

set | less

..and you can explore the current environment variables — use space for paging, or ‘q’ to quit immediately.

Switches to bash for overriding defaults

Bash, as a program, is hardwired to read certain configurations when it starts. However, as is good practice, these can be overridden with command line switches. The switches are passed to bash (more about this later in the article).

Here’s 2 useful switches:


A ‘–noprofile’ option tells bash to not read /etc/profile or any other of the 3 default profile definition files. Still, with ‘–noprofile’ provided, the bash will read its rc configuration files. See below for how to prevent those.

Unless, the rc configuration is prevented with:


A –norc tells bash not to read:

Why /etc/profile and rc files?

The ‘.bashrc’ is a configuration file, whereas ‘profile’ files are more geared towards user’s preferences. I’m by far not an authoritative monk in this, but I’d put technical settings that affect the behavior of bash itself into ‘.bashrc’, while all kinds of things that really affects in a tangible manner the environment into ‘profiles’.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: