The “.bashrc” Environment Variables [4 Cases]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

The “.bashrc” environment variables, an essential feature of Bash let users customize the command-line environment of their system by changing the settings of various commands and utilities. Users can streamline their preferred configuration and improve their command-line experience invariably by leveraging the environment variables in their personalized “.bashrc” file. In this article, you will learn how to set different environment variables in the crucial “.bashrc” file.

Key Takeaways

  • Learning the importance of “.bashrc” for environment variables.
  • Understanding the setting of environment variables in the “.bashrc” file.

Why is “.bashrc” Crucial for Environment Variables?

There are various types of variables in Bash. Among them, environment variables are used to provide information about the current environment and behavior of the program. The “.bashrc” is a user-specific shell script that allows the users to customize their shell environment for interactive Bash sessions by setting these environment variables in multiple ways.

Each time you start a new Bash session, the “.bashrc” file will be executed and the variables set in the file will automatically be loaded. It means you don’t have to set the variables repeatedly. Thus, “.bashrc” plays a crucial role by offering flexibility for personalizing the users’ experience in the Bash shell environment.

4 Cases for Setting Different Environment Variables in the “.bashrc” File

So far, you have got some ideas about the “.bashrc” & the Bash environment variables. Now, I’m going to share several cases of how you can set and customize different environment variables in the “.bashrc” file:

Case 1: Adding a Directory to the “PATH” Variable in the “.bashrc” File

You can modify the “PATH” environment variable in the “.bashrc” file by following the steps below:

Steps to Follow >

➊ Open your Ubuntu Terminal.

➋ Write the command below to view the line for the “PATH” environment variable:

echo $PATH

Viewing the line for the 'PATH' variable using 'echo' command

Alternatively, you can also run the following command to view the contents of the ‘/etc/environment’ file and check the “PATH” variable:

cat /etc/environment

Viewing the contents of the file '/etc/environment' and checking the 'PATH' variable using the 'cat' command

➌ Now, to modify the “PATH” variable, open the ‘.bashrc’ file in the nano text editor, and write the following command:

nano ~/.bashrc

Opening ".bashrc" file in Nano text editor

➍ Then, scroll down the file and include a custom directory to the “PATH” variable at the bottom of the ‘.bashrc’ file using the export command like the following:

export PATH="$PATH:/home/Desktop/Ubuntu"
EXPLANATION
  • export PATH: Remarks a variable as an environment variable.
  • PATH=”$PATH:/home/Desktop/Ubuntu”: The custom directory you want to include. I have modified the “PATH” variable by adding the custom directory ‘/home/Desktop/Ubuntu’ and using a separator ‘:’.

Adding custom directory to 'PATH' variable

➎ Click on CTRL+S to save the file and CTRL+X to exit.

➏ Afterward, to make the changes reset in your current terminal, write the shorthand notation:

. ~/.bashrc

Writing the shorthand notation ". ~/.bashrc " to update the changes

➏ Finally, verify the “PATH” variable by running the echo command:

echo $PATH

Checking if the custom directory is added by using the 'echo' command

From the above image, you can see that the new custom directory has been added, and the “PATH” variable has been updated successfully.

Case 2: Setting a Default Editor in the “.bashrc” File

To set a default editor in the “.bashrc” file follow the steps below:

Steps to Follow >

➊ Open the ‘.bashrc’ file from the terminal and write the following line at the end of the file:

export EDITOR=Nano
EXPLANATION
  • export: Remarks a variable as an environment variable.
  • EDITOR=Nano: Sets the “EDITOR” variable as ‘Nano’.

Setting 'Nano' as default editor in ".bashrc" file

➋ Repeat steps 5 & 6 of case 1 and then, run the “EDITOR” variable with the echo command:

echo $EDITOR

Output showing that the default editor is 'Nano'

The above image shows ‘Nano’ as the value of the default “EDITOR” variable.

Case 3: Defining the Number of Processors in the “.bashrc” File

You can define the number of processors by following the steps below:

Steps to Follow >

➊ Write the following command at the end of the ‘.bashrc’ file:

export PROCESSORS_NUM=3
EXPLANATION
  • PROCESSORS_NUM=3: Sets the “PROCESSORS_NUM” variable as ‘3’.

Setting the number of processors

➋ Repeat steps 5 & 6 of case 1 and run the command below:

echo $PROCESSORS_NUM

Output showing the number of processors

The image displays the value of the “PROCESSORS_NUM” variable.

Case 4: Setting a Custom Environment Variable in the “.bashrc” File

You can easily set a custom environment variable in the ‘.bashrc’ file by following a few steps:

Steps to Follow >

➊ Append the following command at the end of the file:

export CUSTOM_VAR="Hello, custom-variable!"
EXPLANATION
  • CUSTOM_VAR=”Hello, custom-variable!”: I have chosen “CUSTOM_VAR” as the custom variable name and set the value as ‘Hello, custom-variable!’.

Setting a custom variable including a custom value

➋ Then, repeat steps 5 & 6 of case 1 and run the command in the terminal:

echo $CUSTOM_VAR

Output showing the custom variable setting

The output of the above snapshot depicts the value of the custom variable “CUSTOM_VAR”.

Conclusion

To wrap up, the “.bashrc” file is really an essential aspect for users to define, and modify environment variables and manipulate the Bash script with customizing features.

People Also Ask

What is the location of the .bashrc file?
The location of the .bashrc file is in the user’s home directory. In ‘~/.bashrc’, ‘~’ indicates the user’s home directory.

Do I have to restart my system to apply changes to .bashrc?
No, you don’t have to restart your system to apply changes to the .bashrc file.  Instead, you can run ‘. ~/.bashrc’ in the terminal to update the changes.

Is .bashrc file user-specific?
Yes, the ‘.bashrc’ file is use-specific. Changes made in one user’s ‘.bashrc’ file don’t affect the system’s other users ‘.bashrc’ files.

Can I use existing environment variables in .bashrc?
Yes, not only you can use the existing environment variables but also create custom variables in the ‘.bashrc’ file.

Can I define aliases and functions in .bashrc?
Yes, you can define aliases and functions in ‘.bashrc’ to create custom functionalities.

Related Articles


<< Go Back to Types of Variables in Bash | Bash Variables | Bash Scripting Tutorial

Rate this post
Nadiba Rahman

Hello, This is Nadiba Rahman, currently working as a Linux Content Developer Executive at SOFTEKO. I have completed my graduation with a bachelor’s degree in Electronics & Telecommunication Engineering from Rajshahi University of Engineering & Technology (RUET).I am quite passionate about crafting. I really adore exploring and learning new things which always helps me to think transparently. And this curiosity led me to pursue knowledge about Linux. My goal is to portray Linux-based practical problems and share them with you. Read Full Bio

Leave a Comment