
Introduction
Bash (Bourne Again Shell) is the default command-line interpreter for most Linux and UNIX-based systems. It serves as a tool for interacting with the operating system, allowing users to execute commands, manage configurations, and automate repetitive tasks efficiently. Bash provides a wide range of built-in commands and scripting capabilities, enabling users to streamline workflows, manipulate files, and manage system resources with ease.
In this article, you'll learn how to set environment variables in Bash on Linux, allowing you to customize your system, manage application settings, and enhance your workflow efficiently. You can also check out our guide on How to Use Variables in Bash to understand how variables work and how they can be utilized effectively in shell scripting.
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.
Confirm the available Bash version on your system.
console$ bash --version
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.
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/dashThe
$SHELLenvironment variable stores the active shell information. Run the following command to view the active shell in your current session.console$ echo $SHELL
Output:
/bin/shChange 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.
Start a new Bash shell session.
console$ bashPrint the user's active shell environment information.
console$ printenvOutput:
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
exportcommand to make Global variables accessible to all processes in a shell.
Run the following command to list all environment variables in the current shell session.
console$ envOutput:
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 ascd.andcd..._=: 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.
Create a new shell environment variable such as
Helloand setWorldas the value.console$ Hello=World
Retrieve the variable's value using a command like
echo.console$ echo Hello
Output:
WorldConvert the shell variable into an environment variable.
console$ export Hello
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.
Open the
.bashrcfile in the user's home directory.console$ nano ~/.bashrc
Create a new environment variable using the
exportcommand at the end of the file. For example,APP_NAMEand set the value toTest Variable.iniexport APP_NAME="Test Variable"
Save and close the file.
Apply the
.bashrcchanges in your active user's session.console$ source .bashrc
Start a new Bash child session.
console$ bashRetrieve 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.
Create a new global environment variable. For instance,
Hello_Systemand setThis is a global environment variableas the value.console$ export Hello_System="This is a global environment variable"
Open the
/etc/environment/file.console$ sudo nano /etc/environment
Add a new
Hello-Systemenvironment variable at the end of the file.iniHello-System="This is a global environment variable"
Save and close the file.
Apply the changes in your active shell environment.
console$ source /etc/environment
Switch to another user.
console$ su - username
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.