Basic Linux Tutorial
Computer Tutorial

Passing Data Through Environment Variables

Passing Data Through Environment Variables

Shell programs can access environment variables. We can pass data to a program by using environment variables. As you already know, we can change the value of an environment variable in a program, but that change gets lost as soon as the program terminates. In script-04, we get an environment variable COLOR having value red and then change it to green. After execution of the program, when we check the value of the COLOR variable, it is still red.

Example of a simple shell script to Passing and Display the Data Through Environment Variables.


echo   "The current COLOR variable is"
echo   $COLOR
COLOR = green
echo   "The new COLOR variable is"
echo   $COLOR

Before executing this program, you need to set and export the COLOR variable. You can verify the exported value of the variable by using the echo command. After you execute the program, it changes the value and prints the new value green. When the program finishes, you use the echo command again to check the variable value, and you find out that it is the same as it was before executing the program.

$ COLOR = red
$ export COLOR
$ echo $COLOR