Loading...
Shell Script

Environment Variables

An environment variable is a variable that is available to any child process of the shell. Some programs  need environment variables in order to function correctly. Usually a shell script defines only those environment variables that are needed by the programs that it runs.

Normally all the variables are local variables. They can be used in same shell, if another copy of shell is loaded then all the old shells variables are ignored by the new shells.

$ name=Ram
$ echo $name
Ram
$ /bin/bash
$ echo $name

NOTE:- Empty line printed
$ echo $name

$ exit
$ echo $name
Ram

To set an environment variable then export command is used across the environment.

Syntax: export <Variable Name>
Example: export name

$ name=Ram
$ echo $name
Ram
$ export name
$ /bin/bash
$ echo $name
Ram
$ exit
$ echo $name
Ram

In Bash Shell, set command is used to check whether the variable is set to some value or not. To check only the variables which are exported then export-p command is used.

Leave a Reply

Your email address will not be published. Required fields are marked *