Sample shell startup files

The Korn shell .profile and .kshrc

The Korn shell (see ksh(1)) uses two startup files that allow users to customize their shell environment: a $HOME/.profile (if one exists) and the startup file specified by the ENV environment variable (if set and exported). Typically, $HOME/.kshrc is the file name used as the value of the ENV variable, however the startup file for ksh can be named any valid file name and is not constrained to be .kshrc. .kshrc is not a startup file hardcoded into ksh, but rather a common name that is used for the ksh startup file specified (optionally) by the ENV environment variable in the users environment.

The .profile is read once, by your login ksh. The ENV file is read by each invocation of ksh, if ENV is defined in the current environment. Typically, users define ENV in their .profile file, and then this variable is available to all future child shells (unless ENV is unset).

The ENV file is often used for function and alias definitions, and setting ksh options.

A typical Korn shell .profile might look something like this:

1  :
2  #	@(#) profile 23.1 91/04/03
3  #
4  # .profile	-- Commands executed by a login Korn shell
5  #
6  # Copyright (c) 1990-1998 The Santa Cruz Operation, Inc.
7  # All rights reserved.
8  #
9  # This Module contains Proprietary Information of the Santa Cruz
10 # Operation, Inc., and should be treated as Confidential.
11 #
12 PATH=$PATH:$HOME/bin:.			# set command search path
13 export PATH
14 if [ -z "$LOGNAME" ]; then
15  	LOGNAME=`logname`		# name of user who logged in
16  	export LOGNAME
17 fi

18 MAIL=/usr/spool/mail/$LOGNAME # mailbox location 19 export MAIL 20 if [ -z "$PWD" ]; then 21 PWD=$HOME # assumes initial cwd is HOME 22 export PWD 23 fi 24 if [ -f $HOME/.kshrc -a -r $HOME/.kshrc ]; then 25 ENV=$HOME/.kshrc # set ENV if there is an rc file 26 export ENV 27 fi 28 # use default system file creation mask (umask) 29 eval `tset -m ansi:ansi -m $TERM:?${TERM:-ansi} -r -s -Q` 30 # If job control is enabled, set the suspend character to ^Z (control-z): 31 case $- in 32 *m*) stty susp '^z' 33 ;; 34 esac 35 set -o ignoreeof # don't let control-d logout 36 case $LOGNAME in # include command number in prompt 37 root) PS1="!# " ;; 38 *) PS1="!$ " ;; 39 esac 40 export PS1

line 1
Contains a single colon that says ``this is a Bourne shell script.'' Even though this is a startup script for the Korn shell, the authors have chosen to use the more common syntax of the Bourne shell programming language. This single colon command is more portable than the (preferred) newer hash-bang syntax. It is equivalent in function to the line:
#! /bin/sh

lines 2-11
Contain comments.

line 12
Sets the path definition in exactly the same way as the preceding Bourne shell .profile: ``set the path equal to the current path, the bin in the home directory, and the current directory.''

line 13
Exports the path to any subshells. This way, you do not have to include a path definition in your .kshrc.

lines 14-17
Set up a variable called LOGNAME, which is used in the following MAIL setting (line 18). Literally, these lines say ``if checking for the value of LOGNAME returns a zero-length string (that is, if LOGNAME is not set), then set LOGNAME to the output of the logname command. Then, export the LOGNAME variable to all subshells.''

line 18
Tells the shell where to look for mail, using the variable LOGNAME.

line 19
Exports the mail location to all subshells.

lines 20-23
Check to see if a variable is already set, and if it is not, set the variable. These lines are similar to lines 14-17. In this case, PWD is being set to the home directory.

lines 24-27
Check for a .kshrc file in the home directory, and set the ksh variable ENV to this file if it exists. ksh looks in the file pointed to by the ENV variable to set up the environment for every new ksh; you need to tell it explicitly to look in ~/.kshrc for ENV definitions. Literally, these lines say ``if a file called .kshrc exists in the home directory and the file is readable, then set ENV to point to this file, and export ENV.''

line 28
Contains a comment. Just as in the preceding Bourne shell .profile, umask is not set here. The authors have chosen to use the default system umask rather than resetting it on a per-user basis.

line 29
Sets up the terminal type using tset(1), as explained in the preceding Bourne shell .profile.

lines 30-34
Test to see if job control is enabled, and if it is, set the suspend character to <Ctrl>Z. Job control is a Korn shell feature that lets you move jobs you are processing from the foreground to the background and vice versa. You use the suspend character to suspend a job temporarily that is running in the background.

line 35
Tells the shell to ignore single end-of-file (EOF) characters. This is what you set to stop <Ctrl>D from logging you out.

lines 36-40
Set up the prompt based on the value of LOGNAME. For normal users, the prompt is the current command number followed by a ``$''; for root (the superuser), the prompt is the current command number followed by a ``#''.

A typical .kshrc might look like this:

1  :
2  #
3  # .kshrc	-- Commands executed by each Korn shell at startup
4  #
5  #	@(#) kshrc 1.1 90/03/13
6  #
7  # Copyright (c) 1990-1998 The Santa Cruz Operation, Inc.
8  # All rights reserved.
9  #
10 # This Module contains Proprietary Information of the Santa Cruz
11 # Operation, Inc., and should be treated as Confidential.
12 #
13 # If there is no VISUAL or EDITOR to deduce the desired edit
14 #  mode from, assume vi(C)-style command line editing.
15 if [ -z "$VISUAL" -a -z "$EDITOR" ]; then
16	set -o vi
17 fi

line 1
Tells the shell that this is a Bourne shell script by starting the script with a single colon, as you have seen before.

lines 2-14
Contain comments. These make up the bulk of this brief .kshrc.

lines 15-17
Set up vi(1) as the default editor ksh uses when you want to edit a command line. Literally, these lines say ``If the VISUAL variable is not set, and the EDITOR variable is not set, then turn on (set -o) the vi option.''

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 22 April 2004