How to Set Environment Variables in Bash on Linux?
export command followed by the variable name and value, such as export MY_VAR="value". This is the standard method to set environment variables in Bash for the current terminal session. If you’re wondering how to set an environment variable in Linux permanently, add the export command to your ~/.bashrc, ~/.bash_profile, or /etc/environment file, depending on whether you want the change to apply to a single user or the entire system. Then reload the configuration with source ~/.bashrc or restart the terminal to apply the changes.Bash serves as a free software replacement for the original Bourne shell (sh). It has since become the default login shell for most Linux distributions. Bash allows users to execute commands, automate tasks through scripting, and manage system operations efficiently. Its versatility and widespread adoption make it a fundamental tool for system administrators and developers alike. In this blog, we will help you install bash on linux.
1. Bash Installation
Most Linux distributions come with Bash pre-installed. However, if you need to install or update Bash, follow these steps:
Debian-based systems (e.g., Ubuntu)
sudo apt update sudo apt install bash
Red Hat-based systems (e.g., CentOS, Fedora)
sudo yum install bash
Arch Linux
sudo pacman -S bash
For detailed installation instructions, refer to the GNU Bash Reference Manual.
2. Configuring a Bash Shell Environment
Linux systems offer many shell interpreters, with Bash (/bin/bash) being the default for most users. The default shell for each user is specified in the /etc/passwd file. To set up a Bash environment for current user, follow the below steps:
List Available Shells: Display all installed shells on your system
$ 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
Check the Current Shell: Verify the shell you’re presently using:
$ echo $SHELL
Output
/bin/sh
Change the Default Shell to Bash: Use the chsh (change shell) command to set Bash as your default shell:
$ chsh -s /usr/bin/bash
You’ll get a prompt to add your password to confirm the change.
Start a New Bash Session: Launch a new Bash session to start using Bash immediately:
$ bash
Verify the Active Shell: Confirm that Bash is now your active shell:
$ echo $SHELL
Output
/bin/bash
View Environment Variables: Display your current environment variables:
printenv
Sample Output:
SHELL=/bin/bash PWD=/home/username LOGNAME=username HOME=/home/username LANG=en_US.UTF-8 ...
To make environment variable changes permanent, you can add them to your shell’s initialization files, such as ~/.bashrc or ~/.profile. For example, adding export VAR_NAME=value to ~/.bashrc will set VAR_NAME for all future Bash sessions.
3. Set Up a Bash Shell Environment
Configuring your Bash environment enhances your command-line experience. This involves editing initialization files to set up aliases, functions, and environment variables.
User-specific configuration
Edit the ~/.bashrc file for configurations applied to non-login shells
nano ~/.bashrc
Edit the ~/.bash_profile file for login shells
nano ~/.bash_profile
After making changes, apply them
source ~/.bashrc
Or
source ~/.bash_profile
System-wide configuration
Edit the /etc/bash.bashrc file for non-login shells
sudo nano /etc/bash.bashrc
Edit the /etc/profile file for login shells
sudo nano /etc/profile
Apply changes with
source /etc/profile
For more information on Bash configuration files, please refer to the Bash Reference Manual.
4. Create and Set Environment Variables in Bash
In Linux, environment variables can be categorized based on their scope and persistence:
System Environment Variables: These global parameters function universally across all user profiles and system processes. System-wide configuration files such as `/etc/environment`, `/etc/profile`, and `/etc/profile.d/` typically define these variables. Modification of these variables demands administrative credentials which impacts the environmental settings for every user.
User Environment Variables: These variables belong to individual users and are recorded in user-specific profile files like `~/.bashrc`, `~/.bash_profile`, or `~/.zshrc`.. Changes to these variables impact only the individual user’s environment and become active when the user starts a new session.
Understanding these distinctions is crucial for effectively managing and customizing the Linux environment at both the system and user levels.
Environment variables store information used by the shell and applications. Setting them appropriately can influence system behavior and user experience.
Setting a temporary environment variable
export VAR_NAME="value"
This variable will exist only for the duration of the current session.
Add the variable to your ~/.bashrc or ~/.bash_profile file
echo 'export VAR_NAME="value"' >> ~/.bashrc
Apply the changes
source ~/.bashrc
This ensures the variable is set in every new session.
Setting environment variables system-wide
Add the variable to the /etc/environment file
sudo nano /etc/environment
Add the line
VAR_NAME="value"
Reboot the system or apply changes with
source /etc/environment
By installing Bash, configuring your shell environment, and managing environment variables, you can tailor your Linux experience to meet your specific needs. Regularly updating your configurations and understanding how environment variables influence system behavior will enhance your efficiency and control over the system.
FAQs
How do I set an environment variable in Bash?
Use the export command followed by the variable name and value, for example: export MY_VAR="value". This sets the environment variable for the current terminal session.
How do I make an environment variable permanent in Linux?
Add the export command to your ~/.bashrc, ~/.bash_profile, or /etc/environment file, depending on whether you want the variable to apply to a single user or the entire system. Reload the configuration with source ~/.bashrc or restart the terminal.
How can I check if a Bash environment variable is set?
You can check whether a Bash environment variable is set by running echo $VARIABLE_NAME, printenv VARIABLE_NAME, or env in the terminal. These commands help verify that the variable exists and displays its current value if it has been defined.
What is the difference between a shell variable and an environment variable?
A shell variable is available only within the current shell, while an environment variable is exported and inherited by child processes, making it accessible to applications and scripts.
How do I remove an environment variable in Bash?
Use the unset command followed by the variable name, for example: unset MY_VAR. If the variable was added to a configuration file, remove that entry as well to prevent it from being recreated in future sessions