Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
Shells: User's Guide: HP 9000 Computers > Chapter 23 Advanced Concepts and Commands

The ENV Variable

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Index

In Chapter 16 “Starting and Stopping the Shell” the ENV variable was discussed. The ENV variable specifies a file, usually .kshrc, which is executed whenever you spawn a new, interactive POSIX or Korn Shell. An interactive shell, is a shell that has input and output tied directly to the terminal. Therefore, you can access standard input, standard output, and standard error. To determine whether or not your shell is interactive, type:

$ set -o

and look for:

interactive on

This .kshrc file normally contains commands to set up the POSIX or Korn Shell's environment. However, if this file is exceedingly long, spawning the new shell can be a long process. This complicated ENV variable prevents .kshrc from being read when not in interactive mode.

You can turn off the processing of the ENV file for noninteractive shells with the following in your ~/.profile:

export ENV='${FILE[(_$-=1)+(_=0)-(_$-!=_${-%%*i*})]}'
export FILE=$HOME/.envfile

The idea behind this scheme is to set up an array (FILE) whose first element is the file we want executed at startup, and whose second element is null.

$ export FILE=$HOME/.envfile
$ echo $FILE[0]
/users/pbm/.envfile
$ echo $FILE[1]

$

We then want to set ENV to FILE[0] for interactive shells and set ENV to FILE[1] for noninteractive shells. To do this we need an expression that will evaluate to "0" for an interactive shell and will evaluate to "1" for a noninteractive shell.

ENV='${FILE[magic_expression]}'

The flags variable ($-) is the key to forming our "magic_expression". If the shell is interactive, the flags will contain an 'i'. The expression used as the index consists of three parts that are combined to form the final index:

(_$-=1)   (_=0)   (_$- != _${-%%*i*})

Let's look at each part of this expression:

Part 1: (_$-=1)

This creates a parameter named _$- and assigns it a value of 1. The value of this expression is the value of the assignment, namely 1. Note: Since parameter substitution is performed on this expression, the name of the variable will look something like "_ism". To see this, try:

$  echo $-      #  see what flags are
ism
$ echo _$-
_ism
$ echo $_ism
# no variable _ism defined yet
$ ((_$-=1)) # create and assign to _ism
$ echo $_ism # now _ism has a value
1
$
Part 2: (_=0)

Set parameter named _ to 0 (we need this since we may reference $_ in the next step). The value of this expression is 0.

Part 3: (_$- != _${-%%*i*})

This expression checks to see if a parameter named _$- (remember this is _ism in our example, and we have set it to 1 in the first expression) has a different value than the parameter named on the right hand side of the != operator. That parameter name will be either "_" or "_$-" (_ism) depending on the expansion of "${-%%*i*}". The latter will evaluate to null for an interactive shell and to "_$-" for a noninteractive shell.

So, for an interactive shell we have (_$- != _) which is true (value arithmetically speaking is 1) and for a noninteractive shell we have (_$- != _$-_) which is false (value for the expression is 0).

Adding it all up we get the following for an interactive shell:

(_$-=1) + (_=0) - (_$- != _)
1 + 0 - 1 = 0
${FILE[0]} = "$HOME/.envfile"

And for a noninteractive shell:

(_$-=1) + (_=0) - (_$- != _$-)
1 + 0 - 0 = 1
${FILE[1]} = ""
Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1983-1991 Hewlett-Packard Development Company, L.P.