export

>Usage

The export command in Unix is used to set environment variables, making them available to child processes spawned from the current shell session.

When you define a variable in a shell, it is by default local to the shell session. This means the variable is not accessible to any child processes or other shells. However, by using export, you make the variable available to any processes or programs launched from the current shell.

>Synopsis

export [option] [argument]

>Options

VAR=value

The command VAR=value sets a new environment variable and exports it so that it is available to all child processes.
It both assigns the variable and makes it available to any processes spawned from the current shell session.

Example:

Dots

-zsh

> export MY_VAR="Hello, World!"



No output;
The variable is set and exported silently. You can verify it by checking with echo or env:
> echo $MY_VAR
> Hello, World!

-p

The command -p displays all currently exported variables in the shell environment. When you run export -p, you’ll see a list of variables that have been marked for export, along with their values

Example:

Dots

-zsh

> export -p



declare -x HOME="/home/user"
declare -x LANG="en_US.UTF-8"
declare -x MY_VAR="Hello, World!"
declare -x PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin"

VAR+=value

The command VAR+=value is used to append a new value to an existing variable. This allows you to add more content to an environment variable or shell variable without overwriting its current value.

Example:

Dots

-zsh

> VAR="Hello"
> VAR+=" World"
> echo $VAR


Hello World

-f

The command -f command is used to export a function to child processes in a shell. When you define a function in a shell, by default that function is only available in the current shell session. However, if you want that function to be available to any child processes that are spawned from the current shell, you need to export the function using export -f.

Example:

Dots

-zsh

> export -f function_name



No output will be shown when you run export -f.

>Quiz

Progress: Question ? out of ?

Question Title

Page written by Marianna Dinon Spindola