Following describes how Bash executes its startup files.
If files
not exist bash does not give any error. If any of the files exist but cannot be
read, Bash reports an error.
Invoked as an interactive login shell, or with --login
- 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_profile, ~/.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.
- When a login shell exits, Bash reads and executes commands from the file ~/.bash_logout, if it exists.
Interactive shell / non-interactive shell invoked as
interactive using login option ------> /etc/profile -----> .bash_profile/.bash_login/.profile ---on exit ----> .bash_logout
- The .bash_profile file normally has call to .bashrc file. Remember that .bashrc also has calls to /etc/bashrc
Example:
[sukul1@lldd010 ~]$ cat .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
- The /etc/profile also executes the scripts in /etc/profile.d folder.
Following is the piece of code from /etc/profile that does that:
for i in /etc/profile.d/*.sh ; do
if [ -r "$i" ];
then
if [
"${-#*i}" != "$-" ]; then
. "$i"
else
. "$i"
>/dev/null 2>&1
fi
fi
done
Invoked as an interactive non-login
shell
- When an interactive shell that is not a login shell is started, Bash reads and executes commands from ~/.bashrc, if that file exists.
Under tyical setup .bashrc will execute /etc/bashrc file.
Interactive Non-Login Shells ------> .bashrc ----internally executes ----->
/etc/bashrc
Notice the difference. IN case of profile files the /etc/profile
gets executed first.
In case of rcfile, the
/etc/bashrc gets executed later by .bashrc
- Running .bashrc may be inhibited by using the --norc option.
- The --rcfile file option will force Bash to read and execute commands from file instead of ~/.bashrc.
- Note that under typical setup .bash_profile also executes .bashrc. So even login shells execute .bashrc.
Note that a
interactive non-login shell that is run from a login interactive shell will
inherit the variables set (using export) by the login interactive shell in /etc/profile and .bash_profile scripts.
Invoked non-interactively
- When Bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute.
Note that
scripts run from login shells will inherit the variables(export) set in the
login shell (from /etc/profile,.bash_profile)
Invoked with name sh
- If Bash is invoked with the name sh, it tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the posix standard as well.
- When invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first attempts to read and execute commands from /etc/profile and ~/.profile, in that order. The --noprofile option may be used to inhibit this behavior.
- When invoked as an interactive shell with the name sh, Bash looks for the variable ENV, expands its value if it is defined, and uses the expanded value as the name of a file to read and execute. Since a shell invoked as sh does not attempt to read and execute commands from any other startup files, the --rcfile option has no effect.
- A non-interactive shell invoked with the name sh does not attempt to read any other startup files.
Invoked in posix mode
- When Bash is started in posix mode, as with the --posix command line option, it follows the posix standard for startup files. In this mode, interactive shells expand the ENV variable and commands are read and executed from the file whose name is the expanded value.
No other startup files are
read.
What is a interactive shell:
- An interactive shell is one started without non-option arguments, and whose input and error output are both connected to terminals (as determined by isatty(3)), or one started with the -i option.
- An interactive shell generally reads from and writes to a user’s terminal.
- The -s invocation option may be used to set the positional parameters when an interactive shell is started.
- To determine within a startup script whether or not Bash is running interactively, test the value of the ‘-’ special parameter. It contains i when the shell is interactive. For example:
case "$-" in
*i*) echo This shell is interactive ;;
*) echo This shell is not interactive ;;
esac
The $- parameter shows the shell options in effect .
[sukul1@lldd010 ~]$ echo $-
himBH
Alternatively, startup scripts may examine the variable PS1; it
is unset in non-interactive shells, and set in interactive shells. Thus:
if [ -z "$PS1" ]; then
echo This shell is not interactive
else
echo This shell is interactive
fi
The -z is used to test if the shell variable is empty. If yes it
returns true.
Value of PS1 is set only for interactive shells.
If can use this kind of if structure if we want some shell
script to behave differently when invoked in an interactive shell and when
invoked in a script.
No comments:
Post a Comment