I realize there are /etc/profile
and /etc/bashrc
files for setting global environment variables and maybe I'm just misunderstanding their purposes, but...
Is there a global bash_profile
file?
I'm using Mac OS X
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityIt's not called bash_profile
, but the standard place for global bash configuration is /etc/bash.bashrc
. It's usual to call this from /etc/profile
if the shell is bash. For example, in my /etc/profile
I have:
if [ "$PS1" ]; then
if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then
# The file bash.bashrc already sets the default PS1.
# PS1=’0
if [ ‐f /etc/bash.bashrc ]; then
. /etc/bash.bashrc
fi
fi
fi
In terms of usage, /etc/profile
provides system-wide configuration for all Bourne compatible shells (sh, bash, ksh, etc.). There's normally no need for an equivalent /etc/bash_profile
, because the intention of the profile file is to control behaviour for login shells. Normally anything you want to do there is not going to be bash-specific. /etc/bash.bashrc
is bash-specific, and will be run for both login and non-login shells.
To further complicate things, it looks like OS X doesn't even have an /etc/bash.bashrc
. This is probably related to the fact that Terminals in OS X default to running as login shells, so the distinction is lost:
An exception to the terminal window guidelines is Mac OS X’s Terminal.app, which runs a login shell by default for each new terminal window, calling .bash_profile instead of .bashrc. Other GUI terminal emulators may do the same, but most tend not to.
I don't run OS X, so the extent of my knowledge ends there.
/etc/bash.bashrc
from a non-interactive shell. /etc/profile
can be read by non-interactive shell. See Difference between Login Shell and Non-Login Shell?
Sep 23, 2012 at 2:18
/etc/profile
in Ubuntu 12.04. What would you suggest instead if one wanted an /etc/profile
which was executed only under bash, and no other sh-compatible shell?
Sep 23, 2012 at 2:27
/etc/profile
file for my system contains a comment at the top that reads: # System-wide .profile for sh(1)
. Does this mean profile
is sh
specific? Does sh
somehow run before bash
?
sh
is a subset of bash
. /etc/profile
is executed for sh
, bash
, and all other Bourne-compatible shells.
Sep 24, 2012 at 5:31
/etc/bashrc
on the mac, not /etc/bash.bashrc
. It also appears to run for /bin/sh
as well.
Aug 11, 2015 at 23:48
/etc/profile
is the global bash_profile
. There's no file that's specific to bash, bash just reads the standard file read by all Bourne-style shell. That's where you can set system-wide environment variables.
See Is there a ".bashrc" equivalent file read by all shells? for a generic overview of bash's common startup files.
This does not load /etc/profile
, so nothing in /etc/profile.d/
gets loaded (unlike login shells, see the end).
The global file for this is /etc/bashrc or /etc/bash.bashrc (depending on -DSYS_BASHRC=
flag set at compile time):
# System-wide .bashrc file for interactive bash(1) shells.
if [ -z "$PS1" ]; then
return
fi
PS1='\h:\W \u\$ '
# Make bash check its window size after a process completes
shopt -s checkwinsize
[ -r "/etc/bashrc_$TERM_PROGRAM" ] && . "/etc/bashrc_$TERM_PROGRAM"
Usually it is a good idea to leave this file as-is (as much as possible) to avoid conflicts. The strategy I use is similar to the one that login (/etc/profile) shells use.
My strategy is to append a loader to the above file:
# Add new directory analog to /etc/profile.d
mkdir /etc/bashrc.d
# Write the loader to /etc/bash.bashrc (it might be /etc/bashrc on as mentioned above)
cat >> /etc/bash.bashrc << 'EOF'
# I appended this: Load scripts from /etc/bashrc.d
if test -d /etc/bashrc.d; then
for script in /etc/bashrc.d/*.sh; do
test -r "$script" && . "$script"
done
unset item
fi
EOF
Now I can easily add n customizations to the global interactive (bash) shell file by putting new .shfiles into the /etc/bashrc.d directory.
/etc/bashrc.d/grep.sh
alias grep='grep --color=auto'
You can do this with this one-liner:
printf "alias grep=\'grep --color=auto\'" > /etc/bashrc.d/grep.sh
If, after reading this, you remain unconvinced, do this to convince yourself:
printf "alias grep=\'grep --color=auto\'" > /etc/profile.d/grep.sh
Open a new terminal emulator and search for some common word in your user directory like "the" using grep
grep -r 'the'
Nothing should be colored. Do the same thing in a virtual console by doing CTRLALTF1 (remember that your X server is probably running at CTRLALTF7 so that you can switch back. If your forget, just restart your desktop manager, for example)
grep -r 'the'
will yield colored results as desired.
Nothing should be colored. Do the same thing in a virtual console by doing CTRLALTF1 (remember that your X server is probably running at CTRLALTF7 so that you can switch back. If your forget, just restart your desktop manager, for example)
grep -r 'the'
will yield colored results as desired.
A login shell is what you get when you boot a machine or switch virtual consoles with CTRLALTF1 through FNth virtual console.
A Login Shell loads /etc/profile
while loads /etc/profile.d/*.sh
files.
Alternatively, you may set the variable in your local .bashrc file using the keyword "export"; for example: export HI='Hello_World'