Understanding User Startup Scripts - /etc/profile, $HOME/.profile, $HOME/.kshrc
User Startup Scripts
When a user logs in using sh or ksh, configuration scripts run in the following order:
- $HOME/.kshrc (for Korn shell (ksh) users if the ENV variable is set)
The /etc/profile Script
- You set up /etc/profile.
- It’s the same file for all users of a system.
- Generally, it contains only a few commands, such as setting a default umask and printing the message of the day.
The $HOME/.profile Script
- Is in a user home directory.
- Is run each time a user logs in.
- Doesn’t exist by default.
- Generally, contains environment variables, such as a user’s preferred editor, the default printer, and the application software location.
$HOME/.profile Script Example
The following example .profile file sets and then exports variables. The MANPATH variable is set to the directories in which the man utility should search for the online manual pages. The ENV variable is set to the full pathname of the file that is read each time a new Korn shell is started. The EDITOR variable is set to the vi editor. Applications, such as cron and edquota, use this variable. Exporting the variables makes them part of the environment, which is inherited by all subshells and subprocesses.
$ cat /.profile # This file initially did NOT exist for root MANPATH=$MANPATH:/usr/share/man:/usr/dt/share/man:/usr/java1.2/man ENV=$HOME/.kshrc EDITOR=vi export MANPATH ENV EDITOR
The ENV variable has special meaning only to the Korn shell. Within a user’s .profile file, often, there will be variables to support software, such as environment variables required by database software that specify the location of the software or the software libraries.
The following three lines are special because they allow a root user to change the shell from Bourne to Korn after the initial login. This benefits a root user who wants the default to be Bourne in case of a system catastrophe when only the root file system is available. If there is no failure, the shell then changes to Korn, in which the history, alias, or other features of the Korn shell are available. Therefore, refine the .profile file to test for the existence of /usr/bin/ksh, and only then execute these lines if the file exists.
SHELL=/usr/bin/ksh # This variable sets the default shell # for subshells export SHELL /usr/bin/ksh # Invokes a Korn shell as a child of the login shell
The $HOME/.kshrc Script
The Korn shell executes the $HOME/.kshrc file if the ENV variable is set. This happens each time a Korn shell is spawned, either at the request of a user or when a shell spawns a Korn subshell. This file resides in the user’s home directory. The file sets Korn shell specific parameters, variables, and aliases.
$HOME/.kshrc Script Example
The following .kshrc script first sets the PS1 variable, which is the prompt. The exclamation point within the string expands to the current command number in the history file. Setting the trackall option causes the Korn shell to execute commands faster by creating aliases for commands for which it has to search. Aliases are established to save keystrokes.
$ cat .kshrc PS1="‘hostname‘ ! $ " set -o trackall alias l=’ls -laF’ alias ll=’ls -aF’ alias hi=’fc -l’ alias c=clear
Modifying a Configuration File
If you make changes to the /etc/profile or $HOME/.profile file, the shell does not read the changes until the next time you log in. To avoid having to re-log in, use the dot command to tell the shell to reread the .profile file:
$ . $HOME/.profile
By invoking a script with the dot command, you tell the shell (parent) to read the commands in the script_name file and execute them without spawning a child process. Any definitions in the script then become part of the present environment.
Most shell scripts execute in a private environment. Any variables they set are not available to other scripts. The shell (or script) that invokes a script is called a parent, and the script that is being invoked is called the child. Variables are not inherited unless they are exported with the export command.