Environment Variable
Environment Variable
• Global environment variables are visible from the shell session, and any
child processes that the shell spawns.
• Local variables are only available in the shell that creates them.
• This makes global environment variables useful in applications that spawn
child processes that require information from the parent process.
• To view the global environment variables, use the printenv command:
$ printenv
• HOSTNAME=testbox.localdomain
• SHELL=/bin/bash
• HISTSIZE=1000
• OLDPWD=/home/rich/test/test1
• SSH TTY=/dev/pts/0
• USER=rich
• MAIL=/var/spool/mail/rich
• PATH=/usr/kerberos/bin:/usr/lib/ccache:/usr/local/bin:/bin:/usr/bin:/home/rich/
bin
• PWD=/home/rich
GLOBAL ENVIRONMENT
VARIABLES(CONTD..)
• To display the value of an individual environment variable,
use the echo command
• When referencing an environment variable, you must place
a dollar sign before the environment variable name:
$ echo $HOME
/home/rich
GLOBAL ENVIRONMENT
VARIABLES(CONTD..)
• As it is mentioned, global environment variables are also
available to child processes running under the current shell
session:
$ bash
$ echo $HOME
/home/rich
LOCAL ENVIRONMENT
VARIABLES
• Local environment variables, as their name implies, can be
seen only in the local process in which they are defined
LOCAL ENVIRONMENT VARIABLES(CONTD..)
• The set command displays all of the environment variables set for a specific
process. However, this also includes the global environment variables.
Here’s the output from a sample set command:
• $ set
BASH=/bin/bash
BASH ARGC=()
BASH ARGV=()
BASH LINENO=()
BASH SOURCE=()
SETTING ENVIRONMENT
VARIABLES
VAR1=456
ravi ()
{
local VAR=123
echo "Local Var: $VAR"
}
ravi
echo "VAR Value $VAR"
echo "Global Variable: $VAR1"