| /etc/profile | /etc/bashrc|

|^|

What is /etc/profile used for?

If you have been using Linux for a while you are probably familiar with the.profile or .bash_profile files in your home directory. These files are used to set environmental items for a users shell. Items such as umask, and variables such as PS1 or PATH.

The /etc/profile file is not very different however it is used to set system wide environmental variables on users shells. The variables are sometimes the same ones that are in the .bash_profile, however this file is used to set an initial PATH or PS1 for all shell users of the system.

/etc/profile.d

In addition to the setting environmental items the /etc/profile will execute the scripts within /etc/profile.d/*.sh. If you plan on setting your own system wide environmental variables it is recommended to place your configuration in a shell script within /etc/profile.d.

What is /etc/bashrc used for?

Like .bash_profile you will also commonly see a .bashrc file in your home directory. This file is meant for setting command aliases and functions used by bash shell users.

Just like the /etc/profile is the system wide version of .bash_profile. The/etc/bashrc for Red Hat and /etc/bash.bashrc in Ubuntu is the system wide version of .bashrc.

Interestingly enough in the Red Hat implementation the /etc/bashrc also executes the shell scripts within /etc/profile.d but only if the users shell is a Interactive Shell (aka Login Shell)

When are these files used?

The difference between when these two files are executed are dependent on the type of login being performed. In Linux you can have two types of login shells, Interactive Shells and Non-Interactive Shells. An Interactive shell is used where a user can interact with the shell, i.e. your typical bash prompt. Whereas a non-Interactive shell is used when a user cannot interact with the shell, i.e. a bash scripts execution.

The difference is simple, the /etc/profile is executed only for interactive shells and the /etc/bashrc is executed for both interactive and non-interactive shells. In fact in Ubuntu the /etc/profile calls the /etc/bashrcdirectly.

Interactive Shell vs Non-Interactive Shell

To show an example of an interactive shell vs a non-interactive shell I will add a variable into both /etc/profile and /etc/bash.bashrc on my Ubuntu system.

/etc/profile

# grep TEST /etc/profile export TESTPROFILE=1

/etc/bash.bashrc

# grep TEST /etc/bash.bashrc export TESTBASHRC=1

Interactive Shell

The below example is showing an interactive shell, in this case both the/etc/profile and /etc/bash.bashrc was executed.

# su - # env | grep TEST TESTBASHRC=1 TESTPROFILE=1

Non-Interactive Shell

In this example we are running a command through SSH that is non-interactive; because this is a non-interactive shell only the /etc/bash.bashrc file is executed.

# ssh localhost "env | grep TEST" root@localhost's password: TESTBASHRC=1

Conclusion

In my case the applications child processes are not recognizing the umask value set in /etc/profile but do recognize the value in /etc/bashrc. This tells me that the subprocess is starting as a non-interactive shell. While the suggested route of modifying environmental variables is to add a shell script into/etc/profile.d in my case it is better to set the umask value in the/etc/bashrc.

source: http://bencane.com/2013/09/16/understanding-a-little-more-about-etcprofile-and-etcbashrc/

FOR PRACTICE:

/etc/profile and /etc/bashrc are similar, but traditionally have different purposes.

As IBall mentioned, the main difference is that /etc/bashrc is not automatically read in under any circumstance. The only way it gets included, is if it's referenced in your ~/.bashrc file with something like:

Code:

if [ -f /etc/bashrc ] ; then . /etc/bashrc fi

/etc/profile is read in automatically only if the shell is a login shell.

Aside from that, they [i]could[/] contain the exact same information, but because of that behavior, they become specialized. /etc/profile contains system/global environment variables and startup programs. Since environment variables are persistent (each process started by a shell inherits them) it's only needed to read them once. Similarly, once a startup program is launched, there is no need to start it again.

Since /etc/bashrc is included by ~/.bashrc, and read every time a shell starts up, people use it to include shell aliases and functions. Aliases and functions do not behave like environment variables; they are not passed to other processes. If you want to see an example, do this at a command prompt:

Code:

alias testecho='echo this is a test!' cat > ~/test_script.bash << EOF #!/bin/bash testecho EOF chmod u+x ~/test_script.bash ~/.test_script.bash

Executing the script in the last line will give you a "testecho: command not found" error because aliases and functions are not passed to sub-processes. So, since ~/.bashrc isincluded with every shell (and implicitly /etc/bashrc), then people use these to set up aliases and functions so all of their shells have the same customizations.

One last bit to note. Both /etc/profile and /etc/bashrc contain settings that can be overwritten by users. As a system administrator, that poses a problem if you want to impose a global change on all users. Of these two files, the only one that lets you make such a change is /etc/profile. The reason for that is, a user can remove the "if [ -f /etc/bashrc ]" line from ~/.bashrc, and any changes to /etc/bashrc will be ignored. /etc/profile is guaranteed to be included at least once when the user initialy logs in, and that leaves it as the only viable option for including system-wide changes.

Last edited by Dark_Helmet; 01-12-2005 at 04:13 AM.

source: http://www.linuxquestions.org/questions/linux-general-1/etc-profile-v-s-etc-bashrc-273992/

EOF