Interview Prep/Shell Scripting/Shells, scripts and execution

What is the difference between a shell variable and an environment variable?

BeginnerConceptShells, scripts and execution
Answer

A shell variable exists only in the current shell. An environment variable is exported and copied into the environment of every child process.

REGION=ap-south-1;  bash -c 'echo "[$REGION]"'   # []
export REGION;      bash -c 'echo "[$REGION]"'   # [ap-south-1]
REGION=us-east-1 aws s3 ls                        # set only for this one command
env | grep REGION                                 # list exported variables

Children can never change the parent's environment, which is why source exists.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Shells, scripts and execution