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.