Export variable

2 snippets across 2 stacks - Bash & Linux, JavaScript

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.

JSJavaScript

Named Exports

JS · Modules
Syntax
export const name = value;
export function fn() { ... }
export { a, b, c };
Example
// mathUtils.js
export const PI = 3.14159;

export function areaOfCircle(radius) {
  return PI * radius ** 2;
}

export function circumference(radius) {
  return 2 * PI * radius;
}

Note Named exports can be many per module. They must be imported by exact name (or renamed with 'as'). Prefer named exports for better tree-shaking.

Frequently asked questions

How do you export variable?
This task is covered in 2 stacks on this page: Bash & Linux, JavaScript. 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 "export variable" on this page?
Bash & Linux, JavaScript. 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.