export

export command

  • A built-in command of the Bash shell

  • Ensure environment variables and functions to be passed to child processes

  • Syntax

    export [-f] [-n] [name[=value] ...] or export -p  
    
    • -f : Names are exported as functions
    • -n : Remove names from export list
    • -p : List of all names that are exported in the current shell

Usage

Display all the exported environment variables of your system

export

Display all exported variable on current shell

export -p

If you want to check specified variable:

export -p | grep <var>

Using export with functions

export -f function_name  

Example

linux-export-command3

Assign a value before exporting a function or variable

export name[=value]  

Set an environment variable

To create a new variable, use the export command followed by a variable name and its value.

Syntax:

export NAME=VALUE  

Example:

export PROJECT_DIR=main/app
export -p | grep PROJECT_DIR
declare -x PROJECT_DIR="main/app"
Variables set directly with export are temporary variables, which means that the value defined for that variable will not take effect when you exit the current shell.

Environment varaible PATH

The PATH variable is an environment variable that contains an ordered list of paths that Unix/Linux will search for executables when running a command. Using these paths means that we do not have to specify an absolute path when running a command.

For example

echo $PATH
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin

Here : is the separator. The PATH variable is itself a list of folders that are “walked” through when you run a command. In this case, the folders on PATH are:

  • /usr/lib/lightdm/lightdm
  • /usr/local/sbin
  • /usr/local/bin

Linux/Unix traverses these folders in order until finding an executable.

Adding a New Path

We can add a new path to the PATH variable using the export command.

To append a new path, we reassign PATH with our new path <new-path> at the end:

export PATH=$PATH:<new-path>

For example, let’s say we’re gonna add a new path usr/local/bin to thePATH variable:

export PATH=$PATH:usr/local/bin

Reference