How to Set Environment Variables in Bash on Linux

Updated on October 11, 2024
How to Set Environment Variables in Bash on Linux header image

Introduction

Bash (Bourne Again Shell) is the default command-line interpreter for most Linux and UNIX-based systems, allowing you to execute commands, manage configurations, and automate tasks. In this article, you'll learn how to set environment variables in Bash to customize your system and improve your workflow.

Install Bash on Linux

Bash is available on most Linux-based operating systems but may be unavailable depending on the distribution release or version. Follow the steps below to install Bash on your operating systems.

  1. Confirm the available Bash version on your system.

    console
    $ bash --version
    
  2. Install Bash on:

    • Ubuntu/Debian:

      console
      $ sudo apt install bash
      
    • CentOS, Rocky Linux, Alma Linux/RHEL distributions:

      console
      $ sudo yum install bash
      
    • Fedora:

      console
      $ sudo dnf install bash
      
    • FreeBSD:

      console
      $ sudo pkg install bash bash-static
      
    • OpenBSD:

      console
      $ doas pkg_add bash
      

Set Up a Bash Shell Environment

Linux includes multiple shell interpreters, including Bash that apply to all system users. Bash is the default shell for most users by default and runs under the /bin/bash executable. The /etc/passwd file contains the default shell information for each user. Follow the steps below to set up a Bash shell environment for your active user.

  1. List all shells available on your server.

    console
    $ cat /etc/shells
    

    Output:

    # /etc/shells: valid login shells
    /bin/sh
    /usr/bin/sh
    /bin/bash
    /usr/bin/bash
    /bin/rbash
    /usr/bin/rbash
    /bin/dash
    /usr/bin/dash
  2. The $SHELL environment variable stores the active shell information. Run the following command to view the active shell in your current session.

    console
    $ echo $SHELL
    

    Output:

    /bin/sh
  3. Change the active shell to Bash using the chsh (Change shell) command.

    console
    $ chsh -s /usr/bin/bash
    

    Enter the user's password when prompted to change the active shell.

  4. Start a new Bash shell session.

    console
    $ bash
    
  5. Print the user's active shell environment information.

    console
    $ printenv
    

    Output:

    SHELL=/bin/bash
    PWD=/home/user
    LOGNAME=user
    HELLO=WORLD
    XDG_SESSION_TYPE=tty
    MOTD_SHOWN=pam
    HOME=/user
    LANG=C.UTF-8
    .......

    To optimize your scripting experience, understanding Bash variables is essential for managing data and configurations efficiently.

Create and Set Environment Variables in Bash

Environment variables in Bash are dynamic key-value pairs that affect how processes run in a shell environment. Each user has a separate shell environment that affect how processes use variables. There are two types of environment variables:

  • Local Environment Variables: Work in the active shell environment and only last for the duration of a shell session. Child shells or processes can't access Local variables.
  • Global Environment Variables: Work in multiple environments and are accessible by the active user's shell session and child processes. You can use the export command to make Global variables accessible to all processes in a shell.
  1. Run the following command to list all environment variables in the current shell session.

    console
    $ env
    

    Output:

    SHELL=/bin/bash
    PWD=/home/user
    LOGNAME=user
    HELLO=WORLD
    XDG_SESSION_TYPE=tty
    MOTD_SHOWN=pam
    HOME=/user
    LANG=C.UTF-8
    .......

    The above list contains all environment and shell variables available in the active user's session. Common environment variables include the following:

    • SHELL: Stores the active shell environment interpreter such as Bash, Zsh, or Dash.
    • LANG: Includes the language and locale settings.
    • USER: Stores the active user's name.
    • HOME: The active user's home directory.
    • PWD: The current working directory.
    • OLDPWD: The user's previous working directory, which allows the use of commands such as cd. and cd...
    • _=: Stores the last executed command.
    • MAIL: Stores the user's mailbox path.
    • PATH: Stores a list of directories and paths the system checks when executing commands.
  2. Create a new shell environment variable such as Hello and set World as the value.

    console
    $ Hello=World
    
  3. Retrieve the variable's value using a command like echo.

    console
    $ echo Hello
    

    Output:

    World
  4. Convert the shell variable into an environment variable.

    console
    $ export Hello
    
  5. List all environment variables in the current session and verify the variable is available.

    console
    $ env | grep Hello
    

    Output:

    Hello=World

Create Persistent Environment Variables

Environment variables work in the active user's session unless you persist them through the .bashrc file which includes customizations for interactive non-login shells. Follow the steps below to create persistent environment variables available to a user when logging in.

  1. Open the .bashrc file in the user's home directory.

    console
    $ nano ~/.bashrc
    
  2. Create a new environment variable using the export command at the end of the file. For example, APP_NAME and set the value to Test Variable.

    ini
    export APP_NAME="Test Variable"
    

    Save and close the file.

  3. Apply the .bashrc changes in your active user's session.

    console
    $ source .bashrc
    
  4. Start a new Bash child session.

    console
    $ bash
    
  5. Retrieve the environment variable's value to verify that it's available to the child process.

    console
    $ echo $APP_NAME
    

    Output:

    Test Variable

Create Global Environment Variables

Global environment variables are accessible by all system users and processes. Follow the steps below to create a system-wide global environment variable.

  1. Create a new global environment variable. For instance, Hello_System and set This is a global environment variable as the value.

    console
    $ export Hello_System="This is a global environment variable"
    
  2. Open the /etc/environment/ file.

    console
    $ sudo nano /etc/environment
    
  3. Add a new Hello-System environment variable at the end of the file.

    ini
    Hello-System="This is a global environment variable"
    

    Save and close the file.

  4. Apply the changes in your active shell environment.

    console
    $ source /etc/environment
    
  5. Switch to another user.

    console
    $ su - username
    
  6. Print the global environment variable's value.

    console
    $ echo $Hello_System
    

    Output:

    This is a global environment variable

Conclusion

You have successfully configured a Bash shell environment in Linux and set environment variables in Bash. Bash utilizes variables to store specific values, settings, or configurations that apply to processes and shell environments. For advanced use cases, you can apply these variables with conditional statements, functions, and loops.