Environment and PATH
Export
Pass a Value to a Child Shell
Shell variables stay local until they are exported. Exported names become environment variables for child processes.
Program
Play the script to choose a profile, export it, and read it from a child shell.
export_child_env.sh
Replay: real traced execution (multi-file project)
#!/usr/bin/env bash
profile="dev"
export APP_PROFILE="$profile"
child_value=$(bash -c 'printf "%s" "$APP_PROFILE"')
echo "child=$child_value"
#!/usr/bin/env bash
profile="test"
export APP_PROFILE="$profile"
child_value=$(bash -c 'printf "%s" "$APP_PROFILE"')
echo "child=$child_value"
#!/usr/bin/env bash
profile="prod"
export APP_PROFILE="$profile"
child_value=$(bash -c 'printf "%s" "$APP_PROFILE"')
echo "child=$child_value"
profile ← dev
3profile="dev"4export APP_PROFILE="$profile"values this stepdevprofileAPP_PROFILE ← dev, exported ← APP_PROFILE
3profile="dev"4export APP_PROFILE="$profile"5child_value=$(bash -c 'printf "%s" "$APP_PROFILE"')values this stepdevAPP_PROFILEAPP_PROFILEexporteddevprofilechild_value ← dev
4export APP_PROFILE="$profile"5child_value=$(bash -c 'printf "%s" "$APP_PROFILE"')6echo "child=$child_value"values this stepdevchild_valuedevAPP_PROFILEecho "child=$child_value"
5child_value=$(bash -c 'printf "%s" "$APP_PROFILE"')6echo "child=$child_value"outputchild=devvalues this stepdevchild_value
profile ← test
3profile="test"4export APP_PROFILE="$profile"values this steptestprofileAPP_PROFILE ← test, exported ← APP_PROFILE
3profile="test"4export APP_PROFILE="$profile"5child_value=$(bash -c 'printf "%s" "$APP_PROFILE"')values this steptestAPP_PROFILEAPP_PROFILEexportedtestprofilechild_value ← test
4export APP_PROFILE="$profile"5child_value=$(bash -c 'printf "%s" "$APP_PROFILE"')6echo "child=$child_value"values this steptestchild_valuetestAPP_PROFILEecho "child=$child_value"
5child_value=$(bash -c 'printf "%s" "$APP_PROFILE"')6echo "child=$child_value"outputchild=testvalues this steptestchild_value
profile ← prod
3profile="prod"4export APP_PROFILE="$profile"values this stepprodprofileAPP_PROFILE ← prod, exported ← APP_PROFILE
3profile="prod"4export APP_PROFILE="$profile"5child_value=$(bash -c 'printf "%s" "$APP_PROFILE"')values this stepprodAPP_PROFILEAPP_PROFILEexportedprodprofilechild_value ← prod
4export APP_PROFILE="$profile"5child_value=$(bash -c 'printf "%s" "$APP_PROFILE"')6echo "child=$child_value"values this stepprodchild_valueprodAPP_PROFILEecho "child=$child_value"
5child_value=$(bash -c 'printf "%s" "$APP_PROFILE"')6echo "child=$child_value"outputchild=prodvalues this stepprodchild_value
export
`export NAME=value` places a shell variable into the environment.
child process
A child shell inherits exported variables from the parent shell.
local variable
Without export, ordinary shell variables are not part of the child environment.