How to use the command 'export' (with examples)

How to use the command 'export' (with examples)

The ’export’ command in Bash is used to mark shell variables in the current environment to be exported with any newly forked child processes. This allows these variables to be accessed and used by other processes or subshells. In this article, we will explore different use cases of the ’export’ command with examples.

Use case 1: Set a new environment variable

Code:

export VARIABLE=value

Motivation: Setting a new environment variable can be useful when we want to define a value that can be accessed by other processes or scripts.

Explanation:

  • ’export’: The ’export’ command is used to mark the variable for export.
  • ‘VARIABLE’: This is the name of the variable we want to set.
  • ‘value’: The value we want to assign to the variable.

Example:

export MY_NAME="John"
echo $MY_NAME

Output:

John

In the above example, we set a new environment variable named ‘MY_NAME’ with the value ‘John’. We then use the ’echo’ command to print the value of the variable, which is ‘John’.

Use case 2: Remove an environment variable

Code:

export -n VARIABLE

Motivation: Sometimes, we may want to remove an environment variable that is no longer needed.

Explanation:

  • ’export’: The ’export’ command is used to remove the export attribute from the variable.
  • ‘-n’: This option is used to remove the value of the variable.

Example:

export MY_NAME="John"
echo $MY_NAME

export -n MY_NAME
echo $MY_NAME

Output:

John

In the above example, we first set the ‘MY_NAME’ environment variable to ‘John’ and print its value. Then, we use the ’export -n’ command to remove the value of the variable. When we try to print the value again, it shows an empty output.

Use case 3: Mark a shell function for export

Code:

export -f FUNCTION_NAME

Motivation: Marking a shell function for export allows it to be accessed and used by other scripts or processes.

Explanation:

  • ’export’: The ’export’ command is used to mark the shell function for export.
  • ‘-f’: This option is used to specify that we want to export a shell function.
  • ‘FUNCTION_NAME’: This is the name of the shell function we want to export.

Example:

my_function() {
  echo "Hello, world!"
}

export -f my_function

bash -c 'my_function'

Output:

Hello, world!

In the above example, we define a shell function named ‘my_function’ that prints “Hello, world!”. We then use the ’export -f’ command to mark the function for export. Finally, we use the ‘bash -c’ command to execute a new Bash shell and call the exported function within it.

Use case 4: Append something to the PATH variable

Code:

export PATH=$PATH:path/to/append

Motivation: Appending a path to the PATH variable allows us to include additional directories for executable files.

Explanation:

  • ’export’: The ’export’ command is used to export the modified PATH variable.
  • ‘PATH’: This is the name of the environment variable we want to modify.
  • ‘$PATH’: This expands the current value of the PATH variable.
  • ‘path/to/append’: This is the additional directory or path we want to append to the PATH variable.

Example:

export PATH=$PATH:/usr/local/bin
echo $PATH

Output:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/bin

In the above example, we append the ‘/usr/local/bin’ directory to the existing PATH variable. After exporting the modified variable, we print the value of the PATH variable. As a result, we can see that the ‘/usr/local/bin’ directory is successfully added to the PATH.

Conclusion:

The ’export’ command in Bash is a powerful tool for managing environment variables. It allows us to set new variables, remove existing ones, mark shell functions for export, and modify variables like PATH. Understanding and utilizing the various use cases of the ’export’ command can enhance our scripting and shell environment management skills.

Related Posts

How to use the command 'feroxbuster' (with examples)

How to use the command 'feroxbuster' (with examples)

The ‘feroxbuster’ command is a simple and fast content discovery tool written in Rust.

Read More
How to use the command systemd-sysusers (with examples)

How to use the command systemd-sysusers (with examples)

The systemd-sysusers command is a utility that allows users to create system users and groups.

Read More
How to use the command rustup toolchain (with examples)

How to use the command rustup toolchain (with examples)

This command allows you to manage Rust toolchains, such as installing or updating a toolchain, uninstalling a toolchain, listing installed toolchains, and creating a custom toolchain by linking to a directory.

Read More