How to Set an Environment Variable in Bash

In Bash, environment variables are used to store information that can be accessed by the shell and other programs. Setting environment variables allows you to customize the behavior of your shell and applications. This guide will explain how to set environment variables in Bash, including examples and best practices.

1. Basic Syntax for Setting an Environment Variable

The basic syntax for setting an environment variable in Bash is as follows:

export VARIABLE_NAME="value"

In this syntax:

  • VARIABLE_NAME is the name of the environment variable you want to create or modify.
  • "value" is the value you want to assign to the variable. It is enclosed in quotes if it contains spaces or special characters.

2. Example of Setting an Environment Variable

Here’s a simple example of setting an environment variable:

export MY_VAR="Hello, World!"

In this example:

  • The command creates a new environment variable named MY_VAR and assigns it the value "Hello, World!".
  • After setting the variable, you can access it using the $ symbol:
  • echo $MY_VAR
  • This command will output: Hello, World!

3. Setting Multiple Environment Variables

You can set multiple environment variables in a single command by using the export command multiple times:

export VAR1="Value1"
export VAR2="Value2"

In this example:

  • Two environment variables, VAR1 and VAR2, are set with their respective values.

4. Setting Environment Variables for the Current Session

When you set an environment variable using the export command, it is available only in the current shell session. If you open a new terminal session, the variable will not be set.

Example of Setting a Variable for the Current Session

export SESSION_VAR="This is a session variable"

In this example:

  • The variable SESSION_VAR is set for the current session only.
  • To verify, you can use echo $SESSION_VAR in the same terminal session.

5. Setting Environment Variables Permanently

If you want to set an environment variable permanently, you can add the export command to your shell's configuration file, such as .bashrc or .bash_profile.

Example of Setting a Variable Permanently

echo 'export PERMANENT_VAR="This is a permanent variable"' >> ~/.bashrc

In this example:

  • The command appends the export statement to the .bashrc file in your home directory.
  • After adding the line, you need to reload the configuration file to apply the changes:
  • source ~/.bashrc

6. Unsetting an Environment Variable

If you need to remove an environment variable, you can use the unset command:

 unset MY_VAR

In this example:

  • The command removes the MY_VAR environment variable from the current shell session.

7. Conclusion

Setting environment variables in Bash is a straightforward process that allows you to customize your shell environment and the behavior of applications. By understanding how to set, access, and unset these variables, you can effectively manage your system's configuration and enhance your scripting capabilities. Whether you need temporary settings for a session or permanent configurations, mastering environment variables is essential for any Bash user.