How to Set Environment Variables in Bash on Linux?
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.
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.