Fundamentals 2 min read

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.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Understanding Shell Global and Local Variables

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.

ShellUnixBashEnvironment Variablesglobal variablelocal variable
DevOps Cloud Academy
Written by

DevOps Cloud Academy

Exploring industry DevOps practices and technical expertise.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.