Set env

2 snippets across 2 stacks - Bash & Linux, Docker

SHBash & Linux

Environment Variables

SH · System Info
Syntax
env
export VAR=value
printenv VAR
Example
env | grep PATH
export DATABASE_URL='postgres://user:pass@db:5432/mydb'
printenv HOME
Output
/home/deploy

Note env lists all environment variables. export makes a variable available to child processes. Variables set without export are local to the current shell. printenv retrieves a single variable. Avoid putting secrets in environment variables on shared systems; prefer a secrets manager.

DKDocker

Environment Variables (-e)

DK · Run Options
Syntax
docker run -e <KEY>=<value> <image>
docker run --env-file <file> <image>
Example
docker run -e NODE_ENV=production -e DB_HOST=db myapp:1.0
docker run --env-file .env myapp:1.0

Note Using --env-file keeps secrets out of your shell history and process list. Each line in the file should be KEY=value with no quotes needed around the value.

Frequently asked questions

How does Bash & Linux handle set env?
This task is covered in 2 stacks on this page: Bash & Linux, Docker. The "Environment Variables" snippet in Bash & Linux uses `env`.
Which command does the Bash & Linux example use?
The "Environment Variables" snippet uses `env`, from the System Info section of the Bash & Linux cheat sheet.
Which stacks cover "set env" on this page?
Bash & Linux, Docker. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Environment Variables": env lists all environment variables. export makes a variable available to child processes. Variables set without export are local to the current shell. printenv retrieves a single variable. Avoid putting secrets in environment variables on shared systems; prefer a secrets manager.