CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Environment & Shell / export

export

Pass variables to the programs you start

Beginneressentialenvironmentshell

export marks a shell variable so it's copied into the environment of every command the shell starts from then on. It's how you hand a database URL, AWS profile or extended PATH to your app, kubectl or terraform.

Syntaxexport [-n] [-f] NAME[=VALUE]... | export -p

Watch it run

Your app can't see DB_HOST even though echo shows it. Find out why.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
DB_HOST=db.internal; echo $DB_HOST; bash -c 'echo "child sees: [$DB_HOST]"'

Your shell knows DB_HOST, but it's only a shell variable. The child bash (standing in for your app) gets an environment without it.

Anatomy of a command

$
commandexport

A shell builtin: it changes the shell's own variables, so it can't be a separate program.

How it works

bash keeps every variable in its own memory with attributes such as readonly, integer and exported. export sets the exported attribute (it's the same as declare -x). Nothing is sent anywhere at that moment. When bash later runs a program, it forks, and in the child calls execve with an envp array built from all variables marked exported. The new program gets a copy of those strings. Because it's a copy, a child can never change its parent's variables: a script that exports something affects only itself and its own children, and the change is gone when it exits. That's why settings meant for your current shell go through source, and settings meant to survive logins go in ~/.profile or ~/.bashrc (or /etc/environment, which pam_env reads at login). sudo resets the environment by default (env_reset in sudoers), so exported variables usually don't reach commands run with sudo.

export only sets a flag; at execve the shell copies flagged variables to the child, and nothing ever flows back.

DB_HOST=xexportrun ./appexeccopiedexport Ysudo cmdresetYour bashPID 2001Shell varsDB_HOST (local)not exportedExport listflag -xfork + execveargv, envpChild: ./appPID 2150Child enva copysudoenv_resetRoot's envminimal, safe
1/7
A plain assignment stays in the shell

DB_HOST=db.internal stores the value in bash's own memory. It's a shell variable: echo sees it, nothing else does.

← → to step

Options you'll actually use

export NAME=VALUESet and export in one step
export NAMEExport an existing shell variable
export -pList exported variables (as declare -x lines)
export -n NAMERemove the export flag; the shell variable stays
export -f FUNCExport a bash function to child bash processes
NAME=VALUE cmdNot export: set it for one command only
declare -x / declare -p NAMESame as export / show a variable and its attributes

Common mistakes

export PATH=/opt/app/bin

It replaces the whole PATH, so ls, grep and everything else become 'command not found' in this shell.

export PATH="$PATH:/opt/app/bin"
export DB_HOST = db.internal

Spaces turn it into three words; bash complains `=': not a valid identifier.

export DB_HOST=db.internal
Running ./set-env.sh to set variables for your session

The script runs in a child process; its exports vanish when it exits.

source ./set-env.sh
export AWS_SECRET_ACCESS_KEY=... typed at the prompt

It's saved in ~/.bash_history and passed to every program you start.

Use aws configure / an SSO profile, or prefix with a space when HISTCONTROL=ignorespace

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Create a variable APP_ENV=staging without exporting it, and prove a child shell can't see it. Then export it and prove it can.

  2. 2

    Add ~/app/scripts to the end of your PATH for this session, then run deploy.sh by name.

  3. 3

    List all exported variables and count them.

  4. 4

    Show that sudo drops an exported variable, then pass it through.

Related commands

envShow the environment, or run a command with a changed onesourceRun a file's commands in the current shellunsetRemove variables and functions from the shellsudoRun a command as root or another user
PreviousenvNextalias
Linux / Environment & Shell / export

export

Pass variables to the programs you start

Beginneressentialenvironmentshell

export marks a shell variable so it's copied into the environment of every command the shell starts from then on. It's how you hand a database URL, AWS profile or extended PATH to your app, kubectl or terraform.

Syntaxexport [-n] [-f] NAME[=VALUE]... | export -p

Watch it run

Your app can't see DB_HOST even though echo shows it. Find out why.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
DB_HOST=db.internal; echo $DB_HOST; bash -c 'echo "child sees: [$DB_HOST]"'

Your shell knows DB_HOST, but it's only a shell variable. The child bash (standing in for your app) gets an environment without it.

Anatomy of a command

$
commandexport

A shell builtin: it changes the shell's own variables, so it can't be a separate program.

How it works

bash keeps every variable in its own memory with attributes such as readonly, integer and exported. export sets the exported attribute (it's the same as declare -x). Nothing is sent anywhere at that moment. When bash later runs a program, it forks, and in the child calls execve with an envp array built from all variables marked exported. The new program gets a copy of those strings. Because it's a copy, a child can never change its parent's variables: a script that exports something affects only itself and its own children, and the change is gone when it exits. That's why settings meant for your current shell go through source, and settings meant to survive logins go in ~/.profile or ~/.bashrc (or /etc/environment, which pam_env reads at login). sudo resets the environment by default (env_reset in sudoers), so exported variables usually don't reach commands run with sudo.

export only sets a flag; at execve the shell copies flagged variables to the child, and nothing ever flows back.

DB_HOST=xexportrun ./appexeccopiedexport Ysudo cmdresetYour bashPID 2001Shell varsDB_HOST (local)not exportedExport listflag -xfork + execveargv, envpChild: ./appPID 2150Child enva copysudoenv_resetRoot's envminimal, safe
1/7
A plain assignment stays in the shell

DB_HOST=db.internal stores the value in bash's own memory. It's a shell variable: echo sees it, nothing else does.

← → to step

Options you'll actually use

export NAME=VALUESet and export in one step
export NAMEExport an existing shell variable
export -pList exported variables (as declare -x lines)
export -n NAMERemove the export flag; the shell variable stays
export -f FUNCExport a bash function to child bash processes
NAME=VALUE cmdNot export: set it for one command only
declare -x / declare -p NAMESame as export / show a variable and its attributes

Common mistakes

export PATH=/opt/app/bin

It replaces the whole PATH, so ls, grep and everything else become 'command not found' in this shell.

export PATH="$PATH:/opt/app/bin"
export DB_HOST = db.internal

Spaces turn it into three words; bash complains `=': not a valid identifier.

export DB_HOST=db.internal
Running ./set-env.sh to set variables for your session

The script runs in a child process; its exports vanish when it exits.

source ./set-env.sh
export AWS_SECRET_ACCESS_KEY=... typed at the prompt

It's saved in ~/.bash_history and passed to every program you start.

Use aws configure / an SSO profile, or prefix with a space when HISTCONTROL=ignorespace

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Create a variable APP_ENV=staging without exporting it, and prove a child shell can't see it. Then export it and prove it can.

  2. 2

    Add ~/app/scripts to the end of your PATH for this session, then run deploy.sh by name.

  3. 3

    List all exported variables and count them.

  4. 4

    Show that sudo drops an exported variable, then pass it through.

Related commands

envShow the environment, or run a command with a changed onesourceRun a file's commands in the current shellunsetRemove variables and functions from the shellsudoRun a command as root or another user
PreviousenvNextalias