When you log in, the shell defines your user environment after reading the initialization files that you have set up. The characteristics of your user environment are defined by the values given to your environment variables. You maintain this environment until you log off the system.
The shell uses two types of profile files when you log in to the operating system. It evaluates the commands contained in the files and then executes the commands to set up your system environment. The files have similar functions except that the /etc/profile file controls profile variables for all users on a system whereas the .profile file allows you to customize your own environment.
The shell first evaluates the commands contained in the /etc/profile file and then runs the commands to set up your system environment in the /etc/environment file.
After these files are run, the system then checks to see if you have a .profile file in your home directory. If the .profile file exists, it runs this file. The .profile file will specify if there also exists an environment file. If an environment file exists, (usually called .env), the system then runs this file and sets up your environment variables.
The /etc/profile, /etc/environment, and the .profile files are run once at login time. The .env file, on the other hand, is run every time you open a new shell or a window.
The first file that the operating system uses at login time is the /etc/profile file. This file controls systemwide default variables such as:
The system administrator configures the profile file for all users on the system. Only the system administrator can change this file.
An example of the /etc/profile file is shown in the following:
# @(#)27 1.20 src/bos/etc/profile/profile, cmdsh, bos430, 9737A_430 8/9/ 94 12:01:38 # IBM_PROLOG_BEGIN_TAG .......... ( Lines Removed ) # (C) COPYRIGHT International Business Machines Corp. 1989, 1994 # All Rights Reserved # Licensed Materials - Property of IBM # # US Government Users Restricted Rights - Use, duplication or # disclosure restricted by GSA ADP Schedule Contract with IBM Corp. # ################################################################ # System wide profile. All variables set here may be overridden by # a user's personal .profile file in their $HOME directory. However, # all commands here will be executed at login regardless. trap "" 1 2 3 readonly LOGNAME # Automatic logout, include in export line if uncommented # TMOUT=120 # The MAILMSG will be printed by the shell every MAILCHECK seconds # (default 600) if there is mail in the MAIL system mailbox. MAIL=/usr/spool/mail/$LOGNAME MAILMSG="[YOU HAVE NEW MAIL]" # If termdef command returns terminal type (i.e. a non NULL value), # set TERM to the returned value, else set TERM to default lft. TERM_DEFAULT=lft TERM=`termdef` TERM=${TERM:-$TERM_DEFAULT} # If LC_MESSAGES is set to "C@lft" and TERM is not set to "lft", # unset LC_MESSAGES. if [ "$LC_MESSAGES" = "C@lft" -a "$TERM" != "lft" ] then unset LC_MESSAGES fi export LOGNAME MAIL MAILMSG TERM trap 1 2 3
The second file that the operating system uses at login time is the /etc/environment file. The /etc/environment file contains variables specifying the basic environment for all processes. When a new process begins, the exec subroutine makes an array of strings available that have the form Name=Value. This array of strings is called the environment. Each name defined by one of the strings is called an environment variable or shell variable. The exec subroutine allows the entire environment to be set at one time.
When you log in, the system sets environment variables from the environment file before reading your login profile, .profile. The following variables make up the basic environment:
An example of the /etc/environment file is shown in the following example:
# @(#)18 1.21 src/bos/etc/environment/environment, cmdsh, bos430, 9737A_ 430 5/13/94 15:09:03 # IBM_PROLOG_BEGIN_TAG .......... ( Lines Removed ) # US Government Users Restricted Rights - Use, duplication or # disclosure restricted by GSA ADP Schedule Contract with IBM Corp. # ################################################################ # System wide environment file. This file should only contain # 1. comment lines which have a # in the first column, # 2. blank lines, and # 3. Lines in the form name=value. # # WARNING: This file is only for establishing environment variables. # Execution of commands from this file or any lines other # than specified above may cause failure of the initialization # process. # # Searching the current directory last is usually a BIG time saver. # If /usr/ucb is at the beginning of the PATH the BSD version of commands will # be found. # PATH=/usr/bin:/etc:/usr/sbin:/usr/ucb:/usr/bin/X11:/sbin TZ=CST6CDT LANG=en_US LOCPATH=/usr/lib/nls/loc NLSPATH=/usr/lib/nls/msg/%L/%N:/usr/lib/nls/msg/%L/%N.cat LC__FASTMSG=true # ODM routines use ODMDIR to determine which objects to operate on # the default is /etc/objrepos - this is where the device objects # reside, which are required for hardware configuration ODMDIR=/etc/objrepos
The third file that the operating system uses at login time is the .profile file. The .profile file is present in your home ($HOME) directory and enables you to customize your individual working environment. Because the .profile file is hidden, use the li -a command to list it.
After the login program adds the LOGNAME (login name) and HOME (login directory) variables to the environment, the commands in the $HOME/.profile file are executed if the file is present. The .profile file contains your individual profile that overrides the variables set in the /etc/profile file. The .profile file is often used to set exported environment variables and terminal modes. You can tailor your environment as desired by modifying the .profile file. Use the .profile file to control the following defaults:
You can also use the .profile file (or if it is not present, the /etc/profile file) to determine login shell variables. You can also customize other shell environments. For example, use the .cshrc file and .kshrc file to tailor a C shell and a Korn shell, respectively, when each type of shell is started.
An Example of a .profile file is shown in the following:
PATH=/usr/bin:/etc:/usr/sbin:/usr/ucb:$HOME/bin:/usr/bin/X11:/sbin:. export PATH if [ -s "$MAIL" ] # This is at Shell startup. In normal then echo "$MAILMSG" # operation, the Shell checks fi # periodically. # pg .profile PATH=/usr/bin:/etc:/usr/sbin:/usr/ucb:$HOME/bin:/usr/bin/X11:/sbin:. export PATH if [ -s "$MAIL" ] # This is at Shell startup. In normal then echo "$MAILMSG" # operation, the Shell checks fi # periodically.
A fourth file that the operating system uses at login time is the .env file, if your .profile has the following line: export ENV=$HOME/.env
The .env file enables you to customize your individual working environment variables. Because the .env file is hidden, use the li -a command to list it. The .env file contains the individual user environment variables that override the variables set in the /etc/environment file. You can tailor your environment variables as desired by modifying your .env file.
An example of the .env file is as follows:
export myid=`id | sed -n -e 's/).*$//' -e 's/^.*(//p'` \ bold=`tput smso` \ norm=`tput rmso` #set prompt: login & system name (reverse video) & path (normal) if [ $myid = root ] then typeset -x PSCH='${bold}#:${norm}\${PWD}> ' PS1="${bold}#:${norm}\${PWD}> " else typeset -x PSCH='>' PS1="${bold}$LOGNAME@$UNAME:${norm}\${PWD}> " PS2=">" PS3="#?" fi export PS1 PS2 PS3 #setup my command aliases alias ls="/bin/ls -CF" \ d="/bin/ls -Fal | pg" \ rm="/bin/rm -i" \ up="cd .."
Note |
---|
When modifying the .env file, ensure that newly created environment variables do not conflict with standard variables such as MAIL, PS1, PS2, and IFS. |
You can also change the environment variable from the command line. These variables will only be valid until you exit the shell.
To change the default # prompt in the command line to a mysystem> prompt enter:
# PS1=mysystem\ \> mysystem> pwd /etc mysystem> ls -l host* -rw-rw-r-- 1 root system 1864 Nov 20 10:44 hosts -rw-r--r-- 1 root system 2060 Aug 25 09:41 hosts.equiv -rw-rw-r-- 1 root system 1906 Aug 25 09:41 hosts.lpd mysystem>
To set the language environment variable to en_UK for one session enter:
# LANG=en_UK # export LANG
To check current environment variable enter:
# env _=/usr/bin/env LANG=en_US LOGIN=root NLSPATH=/usr/lib/nls/msg/%L/%N:/usr/lib/nls/msg/%L/%N.cat inetd_dummy=xxx PATH=/usr/bin:/etc:/usr/sbin:/usr/ucb:/usr/bin/X11:/sbin LC__FASTMSG=true WINDOWID=4194312 LOGNAME=root LOCPATH=/usr/lib/nls/loc USER=root AUTHSTATE=compat DISPLAY=sv1051b:0.0 SHELL=/bin/ksh ODMDIR=/etc/objrepos HOME=/ TERM=aixterm PWD=/ TZ=CST6CDT #