Bash, the popular Unix-like shell, offers a wealth of functionalities, including the ability to work with environment variables. These variables are crucial in configuring the behavior of Bash sessions and scripts. In this article, I will discuss 2 different methods to read environment variables in Bash, helping you optimize your Linux experience and simplify your scripting endeavors. So, let’s dive in!
- Getting an idea of the Environment variables.
- Learning how to read environment variable through command line.
2 Methods to Read Environment Variables in Bash
To display the value of a specific environment variable, use the echo command along with the $ symbol followed by the name of the variable. However, there are multiple ways to call the Environment Variable such as through the command line or script editor. Here in this article, I will cover both methods. So let’s start!
Method 01: Read Environmental Variable Through Bash Command Line
By using the echo command with the appropriate variable name, you can quickly retrieve the value of any environmental variable directly from the command line. This can be handy for troubleshooting, checking configurations, or using the variable’s value in a script or command.
Let’s say I want to know the current user name using the $USER environment variable. To accomplish the task follow the below process.
Steps to Follow >
➋ Write the following command line in your terminal window.
As the image indicates, the name of the current user is miran which I have displayed with the help of an environment variable $USER.
Method 02: Read Environment Variables in Bash Scripts
You can use environment variables within Bash scripts to access and utilize their values. For example, You want to display the path of the home directory and the name of the current user along with some text. Here I will demonstrate the procedure below.
Steps to Follow >
❶ At first, launch an Ubuntu Terminal.
❷ Write the following command to open a file in Nano:
❸ Copy the script mentioned below:
# Accessing the value of the HOME environment variable
echo "My home directory is: $HOME"
# Combining environment variables with text
echo "Welcome, $USER, to the world of Bash scripting!"
❹ Press CTRL+O and ENTER to save the file; CTRL+X to exit.
❺ Use the following command to make the file executable:
chmod u+x env_var_script.sh
❻ Run the script by the following command:
Upon execution, env_var_script returns /home/miran as the home directory and also miran as the user name.
In this article, I have demonstrated some methods to read the environment variable in bash. By utilizing and understanding it, you can proficient your coding skill whenever you need to use the environment variable. However, if you have any questions or queries, feel free to comment below.
People Also Ask
- How to Declare Variable in Bash Scripts? [5 Practical Cases]
- Bash Variable Naming Conventions in Shell Script [6 Rules]
- How to Assign Variables in Bash Script? [8 Practical Cases]
- How to Check Variable Value Using Bash Scripts? [5 Cases]
- How to Use Default Value in Bash Scripts? [2 Methods]
- How to Use Set – $Variable in Bash Scripts? [2 Examples]
- How to Export Environment Variables with Bash? [4 Examples]