# Source global bash config, when interactive but not posix or sh mode
if test “BASH” &&\
test -z “$POSIXLY_CORRECT” &&\
test “${0#-}” != sh &&\
test -r /etc/bashrc
then
# Bash login shells run only /etc/profile
# Bash non-login shells run only /etc/bashrc
# Check for double sourcing is done in /etc/bashrc.
. /etc/bashrc
fi
Check for interactivity performs using BASH variable which have nothing to do with
interactivity but PS1 do and - do too. In fact /etc/bashrc sourced both
interactive and non-interactive shells.
And there is an inaccuracy: BASH login shell run both /etc/profile and
~/.bash_profile. BASH non-login shell run if it interactive ~/.bashrc only, not
/etc/bashrc, to be precise the last one is launched from the previous one.
/etc/bashrc lines 55-67
# Need to redefine pathmunge, it gets undefined at the end of /etc/profile
pathmunge () {
case ":${PATH}:" in
\*:"$1":\*)
;;
\*)
if \[ "$2" = "after" \] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
esac
}
Definition of function which never used it is unset in line 86 the same file. It can be assumed that it is used by scripts in /etc/profile.d, which sourced after definition but before it is unset. But having studied these scripts I have found no calls to pathmunge().
# Path manipulation
if [ "$EUID" = "0" ]; then
pathmunge /usr/sbin
pathmunge /usr/local/sbin
else
pathmunge /usr/local/sbin after
pathmunge /usr/sbin after
fi
In /etc/bashrc we find that pathmunge is only redefined if there is no login shell.
if ! shopt -q login_shell ; then # We're not a login shell
# Need to redefine pathmunge, it gets undefined at the end of /etc/profile
You have studied the profile.d scripts owned by packages installed on your host. Other packages provided for Fedora may still call pathmunge(), i.e. removing this function will break them.
Fedora 42 at least and current 43 have such bash startup files, there are no problems I just read the bash manual and test the knowledge. I don’t understand on backslashes say another words please.
Yes in /etc/profile there are no problems, the function is used, but in /etc/bashrc the definition of the function seems pointless. We can assume some other scripts currently not installed use it.
This is a backslash " \ " I do not have them in my code you posted. Have a look:
54 if ! shopt -q login_shell ; then # We're not a login shell
55 # Need to redefine pathmunge, it gets undefined at the end of /etc/profile
56 pathmunge () {
57 case ":${PATH}:" in
58 *:"$1":*)
59 ;;
60 *)
61 if [ "$2" = "after" ] ; then
62 PATH=$PATH:$1
63 else
64 PATH=$1:$PATH
65 fi
66 esac
67 }
Note that what you posted is quite different than the actual script in /etc/bashrc which follows.
if ! shopt -q login_shell ; then # We're not a login shell
# Need to redefine pathmunge, it gets undefined at the end of /etc/profile
pathmunge () {
case ":${PATH}:" in
*:"$1":*)
;;
*)
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
esac
}
The \ is an escape character as you used it and it removes the special meaning of the following character to the shell and they become literal characters. The script then does not do what is expected.
In the portion you posted from /etc/profile the trailing \ on each line is a line continuation character so that the if then statement only is executed if ALL the 4 test statements are true.
From /etc/profile
# Source global bash config, when interactive but not posix or sh mode
if test "$BASH" &&\
test -z "$POSIXLY_CORRECT" &&\
test "${0#-}" != sh &&\
test -r /etc/bashrc
then
# Bash login shells run only /etc/profile
# Bash non-login shells run only /etc/bashrc
# Check for double sourcing is done in /etc/bashrc.
. /etc/bashrc
fi
I suggest that you read intensively about bash and scripting before you claim the scripts that have been in linux from the beginning are not correct.
The Bash Guide for Beginners would be a good start.
Yes I send wrong definition of function looks like fedora chat system add some stuff on your own. On /etc/profile I know on \ newline sequence, it effectively removes from input stream. Yes I know that all the four statements are tested. But the case is the same BASH variable have no concern to interactivity.
The point of the four statement is clear: test of interactivity, test no POSIX and sh mode (as they do not source /etc/bashrc in fact they know nothing on it at all) and the last test is to test if the file is readable.
# Source global bash config, when interactive but not posix or sh mode
if test "$BASH" &&\
test -z "$POSIXLY_CORRECT" &&\
test "${0#-}" != sh &&\
test -r /etc/bashrc
then
# Bash login shells run only /etc/profile
# Bash non-login shells run only /etc/bashrc
# Check for double sourcing is done in /etc/bashrc.
. /etc/bashrc
fi
Looks like the code is right, but there is no interactivity check as written in comment, but bahs check, $BASH and $BASH_VERSION is often used to bash check. Non-interactive script do not read /etc/bashrc, by default, so there is no point to do such test, but to test if a shell is a bash shell need to be done before reading /etc/bashrc as it is a bash startup file. Scripts located in /etc/profile.d and code in /etc/bashrc do such interactivity tests individually, you make sure it yourself.
So the code is right but the comments need to be fixed and need to be made more accuracy to avoid offending good people.
Accordingly to man bash in the “INVOCATION” chapter I read
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads
and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_pro‐
file, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is
readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
That is: /etc/bashrc is not read by bash unless sourced from /etc/profile or $HOME/.bashrc.
Also, remember that /etc/profile must be compatible with dash and ksh and perhaps also with zsh. Therefore, /etc/bashrc should only be run by bash and not any of the other bourne-like shells.