FUNDAMENTALS A Complete Guide for Beginners
![icon linux](https://linuxsimply.com/wp-content/uploads/2024/05/Linux-Mascot.png)
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
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
➌ Now, to modify the “PATH” variable, open the ‘.bashrc’ file in the nano text editor, and write the following command:
nano ~/.bashrc
➍ 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"
- 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 ‘:’.
➎ 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
➏ Finally, verify the “PATH” variable by running the echo command:
echo $PATH
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
- export: Remarks a variable as an environment variable.
- EDITOR=Nano: Sets the “EDITOR” variable as ‘Nano’.
➋ Repeat steps 5 & 6 of case 1 and then, run the “EDITOR” variable with the echo command:
echo $EDITOR
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
- PROCESSORS_NUM=3: Sets the “PROCESSORS_NUM” variable as ‘3’.
➋ Repeat steps 5 & 6 of case 1 and run the command below:
echo $PROCESSORS_NUM
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!"
- CUSTOM_VAR=”Hello, custom-variable!”: I have chosen “CUSTOM_VAR” as the custom variable name and set the value as ‘Hello, custom-variable!’.
➋ Then, repeat steps 5 & 6 of case 1 and run the command in the terminal:
echo $CUSTOM_VAR
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
Related Articles
- What Are Built-in Variables in Bash [2 Cases With Examples]
- An Ultimate Guide of Using Bash Environment Variables
- String Variables in Bash [Manipulation, Interpolation & Testing]
- What is Variable Array in Bash? [4 Cases]
- An Extensive Exploration of Bash Special Variables [9 Examples]
- What is Boolean Variable in Bash? [3 Cases With Examples]
- What is HereDoc Variable in Bash? [5 Practical Cases]
- What is PS1 Variable in Bash? [3 Customization Examples]
- What is PS3 Variable in Bash? [3 Practical Examples]
<< Go Back to Types of Variables in Bash | Bash Variables | Bash Scripting Tutorial