Display the value of an environment variable
Content |
Tested using printf |
Debian (Etch, Lenny, Squeeze) |
Ubuntu (Hardy, Intrepid, Jaunty, Karmic, Lucid, Maverick, Natty, Oneiric, Precise) |
Objective
To display the value of an environment variable using a POSIX-compatible shell command
Background
Environment variables are name-value pairs that can be used to communicate information from a process to its descendants. They are typically used to provide programs with information about the environment in which they are executing (hence the name). Notable examples include:
DISPLAY |
the local or remote X Window display that should be used by default |
PATH |
the list of paths to search when looking for an executable |
PWD |
the current working directory |
TERM |
the terminal type |
TZ |
the default timezone |
Scenario
Suppose that you are attempting to troubleshoot a problem connected with the X Window System and you need to determine whether the DISPLAY
environment variable has been set correctly.
(This variable specifies the hostname and port number that X client programs should use to connect to the X server. Without it the client programs would not be able to open any windows.)
Method
A robust and portable method for displaying the value of an environment variable is to use the printf
command:
printf "%s\n" "$DISPLAY"
The first argument is a format string which controls the output of printf
. In this instance it calls for a string (provided by the second argument) followed by a newline.
The second argument is quoted to prevent special characters such as asterisks from being expanded. The dollar sign is a sigil which identifies DISPLAY
as a variable. It would be permissible for $DISPLAY
to be written as ${DISPLAY}
, but that is unnecessary in this instance.
Alternatives
Using echo
Tested using echo |
Debian (Etch, Lenny, Squeeze) |
Ubuntu (Hardy, Intrepid, Jaunty, Karmic, Lucid, Maverick, Natty, Oneiric, Precise) |
A less portable but somewhat more convenient method is to use the echo
command:
echo "$DISPLAY"
In most cases this has the same effect as the printf
command above, but if the value contains any backslash characters then the outcome is implementation defined. Be warned that the behaviour of the echo
command does not always correspond to what is described on the echo
man page as many shells provide their own built-in implementation.
Using printenv
Tested using printenv |
Debian (Etch, Lenny, Squeeze) |
Ubuntu (Hardy, Intrepid, Jaunty, Karmic, Lucid, Maverick, Natty, Oneiric, Precise) |
Some operating systems provide a command named printenv
, the purpose of which is to print the value of an environment variable:
printenv DISPLAY
printenv
was historically part of BSD UNIX. It has not been standardised by POSIX, and is not as widely available as printf
or echo
. It is provided by GNU Coreutils (as used by many GNU/Linux distributions).
See also
- Set the value of an environment variable
- Persistently set the value of an environment variable for a given user
- Persistently set the value of an environment variable for all users
Tags: environment | shell