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.
More on Shells, scripts and execution
What is a shell, and how is Bash different from sh, dash and zsh?BeginnerWhat does the shebang line do, and why prefer `#!/usr/bin/env bash` over `#!/bin/bash`?BeginnerWhat is the difference between `./script.sh`, `bash script.sh` and `source script.sh`?BeginnerWhat happens when you type a command name? How does Bash decide what to run?AdvancedWhy must `cd`, `export` and `read` be builtins rather than external programs?IntermediateWhat is the difference between a login, interactive and non-interactive shell, and which startup files run?Intermediate