0% found this document useful (0 votes)
48 views12 pages

Environment Variable

The bash shell uses environment variables to store information about the shell session and working environment. There are two types of environment variables: global variables, which are visible from the shell session and child processes, and local variables, which are only available in the shell that creates them. To view global variables, use the printenv command, and to set variables, assign a value using an equal sign. Local variables are only visible within the current shell, while global variables can also be accessed by child processes.

Uploaded by

Pranav Paste
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views12 pages

Environment Variable

The bash shell uses environment variables to store information about the shell session and working environment. There are two types of environment variables: global variables, which are visible from the shell session and child processes, and local variables, which are only available in the shell that creates them. To view global variables, use the printenv command, and to set variables, assign a value using an equal sign. Local variables are only visible within the current shell, while global variables can also be accessed by child processes.

Uploaded by

Pranav Paste
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

ENVIRONMENT VARIABLES

• The bash shell uses a feature called environment variables to store


information
about the shell session and the working environment (thus the name
environment variables).
This feature also allows you to store data in memory that can be easily
accessed by any program or script running from the shell
ENVIRONMENT
VARIABLES(CONTD..)
• There are two types of environment variables in the bash
shell:
• Global variables
• Local variables
GLOBAL ENVIRONMENT
VARIABLES

• 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

• Setting local environment variables


• Once you start a bash shell (or spawn a shell script), you’re allowed to
create local variables that are visible within your shell process.
• You can assign either a numeric or a string value to an environment variable
by assigning the variable to a value using the equal sign:
$ test=testing
$ echo $test
testing
$
SETTING ENVIRONMENT
VARIABLES(CONTD..)
• If you need to assign a string value that contains spaces, you’ll
need to use a single quotation mark to delineate the beginning
and the end of the string:
$ test=testing a long string
-bash: a: command not found
$ test=’testing a long string’
$ echo $test
testing a long string
SETTING ENVIRONMENT
VARIABLES(CONTD..)
• Once you set a local environment variable, it’s available for use anywhere
within your shell process.
• However, if you spawn another shell, it’s not available in the child shell:
$ bash
$ echo $test
$ exit
exit
$ echo $test
testing a long string
EXAMPLE

VAR1=456
ravi ()
 {        
local VAR=123        
echo "Local Var: $VAR"
}
ravi
echo "VAR Value $VAR"
echo "Global Variable: $VAR1"

You might also like