Understanding Shell Global and Local Variables
This article explains how to display and set environment variables in a Unix shell using commands like env and export, and demonstrates the behavior of global and local variables across parent and child shells with practical bash examples.
Shell scripts distinguish between global and local variables, which can be inspected and manipulated using commands such as env to list environment variables and export to define them.
For example, after running export linux and starting a new bash session, echo $linux outputs 7.2 , showing that the variable is available in the subshell.
Exporting a variable in the parent shell, e.g., export WWW=baidu.com , makes it visible to child shells; echo $WWW returns baidu.com both before and after launching a new bash instance.
Variables set in a child shell also persist after exiting back to the parent: after export aa=bb in the child, echo $aa shows bb , and once the child shell exits, echo $aa in the parent still outputs bb .
These examples illustrate the scope rules of environment variables in Unix shells, highlighting how global variables propagate to child processes while local changes remain within their originating shell.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.