[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.7 Special Shell Variables

Some shell variables should not be used, since they can have a deep influence on the behavior of the shell. In order to recover a sane behavior from the shell, some variables should be unset, but unset is not portable (see section 10.8 Limitations of Shell Builtins) and a fallback value is needed. We list these values below.

CDPATH
When this variable is set cd is verbose, so idioms such as `abs=`cd $rel && pwd`' break because abs receives the path twice.

Setting CDPATH to the empty value is not enough for most shells. A simple path separator is enough except for zsh, which prefers a leading dot:

 
zsh-3.1.6$ mkdir foo && (CDPATH=: cd foo)
/tmp/foo
zsh-3.1.6$ (CDPATH=:. cd foo)
/tmp/foo
zsh-3.1.6$ (CDPATH=.: cd foo)
zsh-3.1.6$

(of course we could just unset CDPATH, since it also behaves properly if set to the empty string).

Life wouldn't be so much fun if bash and zsh had the same behavior:

 
bash-2.02$ mkdir foo && (CDPATH=: cd foo)
bash-2.02$ (CDPATH=:. cd foo)
bash-2.02$ (CDPATH=.: cd foo)
/tmp/foo

Of course, even better style would be to use PATH_SEPARATOR instead of a `:'. Therefore, a portable solution to neutralize CDPATH is

 
CDPATH=${ZSH_VERSION+.}$PATH_SEPARATOR

Note that since zsh supports unset, you may unset CDPATH using PATH_SEPARATOR as a fallback, see 10.8 Limitations of Shell Builtins.

IFS
Don't set the first character of IFS to backslash. Indeed, Bourne shells use the first character (backslash) when joining the components in `"$@"' and some shells then re-interpret (!) the backslash escapes, so you can end up with backspace and other strange characters.

LANG
LC_ALL
LC_TIME
LC_CTYPE
LANGUAGE
LC_COLLATE
LC_NUMERIC
LC_MESSAGES

These must not be set unconditionally because not all systems understand e.g. `LANG=C' (notably SCO). Fixing LC_MESSAGES prevents Solaris sh from translating var values in set! Non-C LC_CTYPE values break the ctype check. Fixing LC_COLLATE makes scripts more portable in some cases. For example, it causes the regular expression `[a-z]' to match only lower-case letters on ASCII platforms. However, `[a-z]' does not work in general even when LC_COLLATE is fixed; for example, it does not work for EBCDIC platforms. For maximum portability, you should use regular expressions like `[abcdefghijklmnopqrstuvwxyz]' that list characters explicitly instead of relying on ranges.

If one of these variables is set, you should try to unset it, using `C' as a fall back value. see 10.8 Limitations of Shell Builtins, builtin unset, for more details.

LINENO
Most modern shells provide the current line number in LINENO. Its value is the line number of the beginning of the current command (see below the output of the here document). The behavior wrt eval differs according to the shell, but, amusingly, not in the case of sub shells:

 
$ cat lineno
echo 1. $LINENO
cat <<EOF
3. $LINENO
4. $LINENO
EOF
( echo 6. $LINENO )
eval 'echo 7. $LINENO'
$ ash lineno
1.
3.
4.
6.
7.
$ bash-2.03 lineno
1. 1
3. 2
4. 2
6. 6
7. 1
$ zsh-3.1.9 lineno
1. 1
3. 2
4. 2
6. 6
7. 7
$ pdksh-5.2.14 lineno
1. 1
3. 2
4. 2
6. 6
7. 0

NULLCMD
When executing the command `>foo', zsh executes `$NULLCMD >foo'. The Bourne shell considers NULLCMD is `:', while zsh, even in Bourne shell compatibility mode, sets NULLCMD to `cat'. If you forgot to set NULLCMD, your script might be suspended waiting for data on its standard input.

status
This variable is an alias to `$?' for zsh (at least 3.1.6), hence read-only. Do not use it.

PATH_SEPARATOR
If it is not set, configure will detect the appropriate path separator for the build system and set the PATH_SEPARATOR output variable accordingly.

On DJGPP systems, the PATH_SEPARATOR environment variable can be set to either `:' or `;' to control the path separator bash uses to set up certain environment variables (such as PATH). Since this only works inside bash, you want configure to detect the regular DOS path separator (`;'), so it can be safely substituted in files that may not support `;' as path separator. So it is recommended to either unset this variable or set it to `;'.

RANDOM
Many shells provide RANDOM, a variable that returns a different integer when used. Most of the time, its value does not change when it is not used, but on IRIX 6.5 the value changes all the time. This can be observed by using set.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by Charlie & on October, 19 2001 using texi2html