Is there any practical difference between unsetting an environment variable and setting it to the empty string?
Difference between setting an environment variable to the empty string and unsetting it
environment-variables
Related Solutions
Your second assignment TEST="SHELL_TEST"
doesn't un-export the variable. It's still marked as "to be inherited by children". And the value inherited by the child is the value currently set in the parent.
In other words, your second assignment doesn't revert the status of TEST
to a shell variable, it's still an environment variable according to that terminology.
You'd have to un-export it for it to become unset in children processes:
$ typeset +x TEST
$ sh -c 'echo $TEST'
$
The TOPCOLORS variable in this top
(also referred to as unixtop) is not available in the version you are using. It first appeared in version 3.6 of this utility, as stated in the COLOR section of the README file. For some reason, it seems that the usual BSD don't include an updated version of top (FreeBSD 9.1 has 3.5beta12).
If you can install or compile an updated version, the man page should give you more information on how to set colours. In version 3.8beta1 that is what it prints:
TOPCOLORS
Specifies colors to use and conditions for which they should be used. At the present time, only numbers in the summary display area can be colored. In a future version it will be possible to highlight numbers in the process display area as well. The environment variable is the only way to specify color: there is no equivalent command line option. Note that the environment variable TOPCOLOURS is also understood. The British spelling takes precedence. The use of color only works on terminals that understand and process ANSI color escape sequences.
The environment variable is a sequence of color specifications, separated
by colons. Each specification takes the form tag=min,max#code
where
tag
is the name of the value to check,min
andmax
specify a range for the value, andcode
is an ANSI color code.
Multiple color codes can be listed and separated with semi-colons. A missing min
implies the lowest possible value (usually 0) and a missing max
implies infinity. The comma must always be present.
When specifying numbers for load averages, they should be multiplied by 100. For example, the specification:
1min=500,1000#31
indicates that a 1 minute load average between 5 and 10 should be displayed in red.
Color attributes can be combined. For example, the specification:
5min=1000,#37;41
indicates that a 5 minute load average higher than 10 should be displayed with white characters on a red background.
A special tag named
header
is used to control the color of the header for process display. It should
be specified with no lower and upper limits, specifically header=,#
followed by the ANSI color code.
You can see a list of color codes recognized by this installation of top
with the -T
option. This will also show the current set of tests used for
color highligting, as specified in the environment.
(end of man page extract)
Colour Codes
The color.h
file from the source code list the tags and codes the utility understands. The tags are:
/*
* These color tag names are currently in use
* (or reserved for future use):
*
* cpu, size, res, time, 1min, 5min, 15min, host
*/
And the color are the ANSI ones:
/*
* Valid ANSI values for colors are:
*
* 0 Reset all attributes
* 1 Bright
* 2 Dim
* 4 Underscore
* 5 Blink
* 7 Reverse
* 8 Hidden
*
* Foreground Colours
* 30 Black
* 31 Red
* 32 Green
* 33 Yellow
* 34 Blue
* 35 Magenta
* 36 Cyan
* 37 White
*
* Background Colours
* 40 Black
* 41 Red
* 42 Green
* 43 Yellow
* 44 Blue
* 45 Magenta
* 46 Cyan
* 47 White
*/
Example
Using the following setup, you can get a glimpse at the possibilities (here for top 3.8beta1 compiled on FreeBSD):
export TOPCOLORS="header=,#1\
:1min=,399#32:1min=400,799#33:1min=800,#31\
:5min=,299#32:5min=300,599#33:5min=600,#31\
:15min=,199#32:15min=200,399#33:15min=400,#31\
:cpu.user=,9#32:cpu.user=10,49#33:cpu.user=50,#31\
:cpu.system=,9#32:cpu.system=10,49#33:cpu.system=50,#31\
:cpu.interrupt=,9#32:cpu.interrupt=10,49#33:cpu.interrupt=50,#31"
A call to top -T
will directly show you if top understands the color setup you've declared. It will also list all the tags understood (which in my case where somewhat different from the content of the color.h
file).
Alternatives
On different UNIX systems, alternatives are used. Under Linux you usually find the top
utility coming from the procps package. You can also install htop which is a nice colourful alternative that is also available on OSX and FreeBSD (where it relies on the Linux layer), but AFAIK not on Solaris.
Related Question
- environment-variables – Difference Between ‘env’ and ‘printenv’
- Unsetting environment variable with an empty name
- Bash – Assign multiple environment variables to a single variable and expand them on command
- Debian – Setting the LANG environment variable for xfce
- Users – Difference Between logname and $LOGNAME
- Linux – How to unset the ‘http_proxy’ environment variable in Python
Best Answer
Each application is free to interpret an unset variable and an empty variable in the same way or not. It is generally a bad idea to give them different meanings, and most applications don't do it, but it happens.
An example in shells themselves is the
IFS
shell variable (which is usually not exported in the environment, but the same principle applies). If unset, the shell behaves as if the value was$' \t\n'
.In a shell script,
$foo
expands to an empty string whetherfoo
is set to an empty value or unset. You can run the shell under thenounset
setting (set -u
orset -o nounset
), in which case the shell reports an error and exits if you try to expand an unset variable. Otherwise, you can distinguish between unset and empty variables with parameter expansion modifiers:${foo:+a}
expands toa
iffoo
is unset or empty and to the empty string otherwise, whereas${foo+a}
expands toa
even iffoo
is set to the empty string.