Your Session Profile
When you log into a computer and start a new Bash session, you might need to type several commands to customize your session. Perhaps you want to change the editing mode from emacs to vi, create new key bindings, or change your command-line prompt. Rather than typing these commands, Bash will run these commands for you each time you log on if they are saved in a special file. This file is called a profile file because it contains the commands to tailor the session to your particular requirements.
The original Bourne shell ran two profile files whenever a user logged on. First, a file called /etc/profile was the general profile file executed for all users. Second, if a file named .profile appeared in the user's home directory, this contained additional commands for each user. Bash mimics this behavior when it is started as sh instead of bash.
Bash extended the principle to run several profile files depending on the circumstances. In addition, Linux distributions often customize the general profile files to run additional commands stored in other scripts.
Bash differentiates between a login session and other instances. Bash runs as a login shell when a user or program first logs in or when a login is simulated with Bash's --login (or -l) switch. A login shell is not necessarily one that presents a user with a prompt. It only indicates that Bash is the top program in the current session, and when Bash completes its run, the login session will be over.
The login_shell shell option is turned on when you are in a login shell. This option can be used to verify that you are in a login shell.
$ shopt login_shell login_shell on
Bash runs as an interactive shell when it is started without a script or when the -i switch is used. An interactive shell presents the user with a command prompt. An interactive shell is not necessarily a login shell. A user can start a non-login interactive shell by typing bash at a Bash prompt, thus starting a new copy of Bash.
Whether the Bash session is interactive or a login shell determines which profile files are used. This way, Bash separates commands specifically for customizing an interactive session from the more general-purpose commands.
The /etc/profile file contains the setup commands and environment changes common to all users. A general profile file might look something like this:
#!/etc/profile # No core files by default ulimit -S -c 0 > /dev/null 2>&1 # HOSTNAME is the result of running the hostname command declare x HOSTNAME=´/bin/hostname´ # No more than 1000 lines of Bash command history declare x HISTSIZE=1000 # If PostgreSQL is installed, add the Postgres commands # to the user's PATH If test r /usr/bin/pgsql/bin ; then declare x PATH="$PATH"":/usr/bin/pgsql/bin" fi # end of general profile
Only the superuser can edit this file. When Bash is used as a login shell, it executes the first file it finds named ~/.bash_profile, ~/.bash_login, or ~/.profile. When a session completes, Bash runs ~/.bash_logout, if it exists.
For example, SuSE Linux uses ~/.profile for the user's profile file. (You can check this by listing the files in your home directory.) By editing this file, you can add commands that will always execute when you log in.
# My profile shopt -s -o emacs # I prefer emacs mode to vi mode date # display the date when I log on
Test the changes by simulating a login with the --login switch.
$ bash --login Wed Feb 6 15:20:35 EST 2002 $ shopt -o emacs emacs on $ logout
Running a new Bash interactive session without logging in will not run the profile file.
$ bash $ logout bash: logout: not login shell: use 'exit' $ exit
Scripts will not normally execute the login profile scripts. Bash will load a profile for scripts if the BASH_ENV environment variable contains a pathname to a file to execute. However, you should avoid using BASH_ENV. Setting the common environment for a set of scripts is usually done with the source command, which is discussed in Chapter 14, "Functions and Script Execution." BASH_ENV has no effect on interactive sessions.
You can stop the running of the login profile files by using the --noprofile switch.
Bash runs a different set of files for interactive sessions that are not also login sessions. Bash looks for a customization script called ~/.bashrc (Bash resources) and executes it instead of the login profile files. Aliased functions are only allowed in the resource files. A different resource file can be specified with the --init-file or --rcfile switch. A typical resource file might contain the following:
# /etc/bashrc # Don't let others write to the user's files umask 002 # Alias ls to provide default switches alias ls='ls qF'
Some distributions add lines to your login profile to run the commands in ~/.bashrc as well. This is not a feature of Bash, but a change made by your Linux distribution. You can add the following lines to the ~/.bashrc file to test its behavior:
# My bashrc Resource File Customizations printf "%s\n" ".bashrc has run"
Test it from the command prompt by starting new sessions. In this case, SuSE Linux always runs the resource file.
$ bash --login .bashrc has run $ logout $ bash .bashrc has run $ exit
As a result, you cannot be certain of the behavior of the resource file without checking your distribution.
Resource files can be suppressed with the --norc switch.
Your distribution can run other files as well during login:
Red Hat and Mandrake Linux separates your customizations, putting functions and aliases in ~/.bashrc and other customization in ~/.bash_profile. Their .bash_profile script will automatically run a .bashrc file as well, if it is present.
SuSE Linux separates your customizations, putting functions and aliases in ~/.bashrc and other customization in ~/.profile. Their .profile script will automatically run a .bashrc file as well, if it is present.
SCO Linux (also called Caldera Linux) uses ~/.profile, which calls ~/.bashrc. It then runs the profile script from ~/etc/config.d/shells to configure the system defaults. When ~/.bashrc runs, it calls /etc/config.d/shells/bashrc to set up the non-interactive defaults. User's personal customizations should be stored in ~/.profile-private and ~/.bashrc-private, respectively.
Likewise, the general profile files can be customized:
Red Hat and Mandrake split customizations into /etc/bashrc and /etc/profile to mimic the behavior of a user's profile files. Further system scripts automatically executed are for specifically installed packages stored in /etc/profile.d.
SuSE Linux automatically set the contents of many files in the /etc directory, including /etc/profile. As a result, personal changes must stored in a separate file called /etc/profile.local or they can be lost when the system is reconfigured or upgraded. Other system scripts automatically executed are /etc/SuSEconfig and scripts for installed software packages stored in /etc/profile.d. SuSE also includes an /etc/profile.dos script to define customizations for users coming from MS-DOS. This is enabled by SuSE configuration software, but it can also be executed from your /etc/profile.local file.
SCO Linux only puts minimum setup information common to any Bourne-based shells in /etc/profile. Common Bash settings should be configured in /etc/config.d/shells/profile and /etc/config.d/shells/bashrc.