45

Till date I used to set my environment variables in the bash.bashrc file. Recently I was told to use the /etc/environment file. Well, both work fine.

So, what is the difference between them?

I googled this and I found "bashrc is used for particular user and environment, system wide". What is meant by system wide here? /etc/bash.bashrc is also applying changes system wide I guess. Correct me if I am wrong. Any kind of help will be appreciated..

5 Answers 5

38

One difference is that /etc/environment contains only variable definitions and doesn't appear to go through any sort of variable expansion/interpolation. Thus, you can't reference variables in definitions. This for instance won't work:

A="else"
B="something $A"

B will literally be something $A, not the expected something else.

See this question.

By the way, the answer you found through Google appears to be referring to a user's ~/.bashrc, rather than the system-wide /etc/bash.bashrc. That may be causing your confusion.

3
  • what is the purpose of ~/.bashrc when there are two files which does the same thing /etc/environment and /etc/bash.bashrc ? Jun 16, 2012 at 8:13
  • 6
    The /etc/bash.bashrc is executed if any user opens a bash. The ~/.bashrc is only executed if the owner of the home directory where it is saved opens a bash. Jun 16, 2012 at 11:31
  • 1
    I know this is ancient, but nobody has addressed the question above. /etc/environment sets environment variables at login, regardless of your shell. /etc/bash.bashrc (and ~/.bashrc) executes (and may set environment variables during that execution) every time a bash shell is opened. But if your login shell is not bash, or you open a different shell, it is not executed. So, if you change settings in /etc/environment and then open a new shell, you don't get the new values. If you change settings and login via ssh localhost of course that's a new login, so you'll get the new values.
    – Auspex
    Aug 19, 2022 at 14:45
22

The /etc/environment file sets the variable system wide for every user on boot. Commands in the /etc/bash.bashrc are is executed if the bash shell is opened by any user. So the variables would not be set unless a bash shell is opened at least one time.

3
  • Sorry i am newbie to linux. whe bash shell will be opened ? and another thing when bash shell is opened will it override variable set in /etc/enviornment or not ? Jun 16, 2012 at 8:09
  • I'm not sure when the /etc/environment is executed exactly but the /etc/bash.bashrc is executed by Ubuntu when you enter your desktop environment. That's why it works if you set the variables there. My guess is that the /etc/environment is executed once while booting before you enter your desktop and that the execution of the /etc/bash.bashrc overwrites the variables. But once again: I'm not sure about this. Jun 16, 2012 at 11:08
  • 4
    It's upon login, not boot!
    – slm
    Oct 26, 2013 at 22:07
7

And as you are asking about "system wide":

Configuration files located in the /etc directory apply to all users on the system. For /etc/bash.bashrc this would mean to all and everything that's using the "Borne Again SHell" aka Bash on that machine. Even if you're the only human using it, there could be "technical users" affected (simply take a look into the /etc/passwd and check how often the term "/bin/bash" is stated there -- or use grep bash /etc/passwd | wc -l, which should give you that number directly (meaning: "grab" all lines containing the string "bash" from the file "/etc/passwd", and send the results ("|") to the command "wc" (word count) to count the lines ("-l").

So for your user, it is much safer to modify ~/.bashrc instead (meaning the file ".bashrc" -- with a leading dot, yes -- in your home-directory, e.g. /home/ankur/.bashrc), which then just affects your user and leaves everything else alone. Files in /etc should only be changed if system-wide changes are really intended.

Besides: Both configurations will be used if they exist. First, the system-wide file (here: /etc/bash.bashrc) is read and "sourced" (it's settings applied to the current session), and then the users /home/username/.bashrc is handled the same, and thus can add to or even change/overwrite settings from the global /etc/bash.bashrc file.

4

Beyond the system wide and user wide scope discussion, one most significant difference is /etc/environment is not a script other than ~/.bashrc.

You cannot dereference variable inside /etc/environment, its variable assignment which takes line value literally (as already mentioned by roadmr).

Your Ubuntu will lock you out if you screw up the $PATH inside /etc/environment by trying to append new path

PATH=$PATH:/new_path

If your Ubuntu Gnome or Unity login page failed in letting you in without complaining wrong password. And you have recently modified /etc/environment, it's most likely the case.

A fix is to login virtual console CTRL+ALT+F1 login console, manually check $PATH, and fix /etc/environment file.

According to this, /etc/environment is loaded by PAM stack, which populates environment variable line by line.

-1

The difference between the two is that the /etc/enivironment file will work for all of the users while the bash.bashrc file will particularly work for that user only. And if you do anything wrong in the /etc/environment file then the consequences may be severe while you can easily undo the changes in the bash.bashrc file by copying the contents of the /etc/environment file. But the first preference is give to the bash.bashrc file and then to /etc/environment file. It is not that if you make changes in the bash.bashrc file then the terminal will give first preference to the local user file (i.e., bash.bashrc) and then to the main file (i.e., /etc/environment).

1
  • I think you are confusing ~/.bashrc and /etc/bash.bashrc files. The first is local to user opening a shell, the second is global to all users opening a shell.
    – Dennis
    Oct 23, 2013 at 0:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .