FUNDAMENTALS A Complete Guide for Beginners
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!
Key Takeaways
- Getting an idea of the Environment variables.
- Learning how to read environment variable through command line.
Free Downloads
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 >
➊ At first, press CTRL + ALT + T to launch the Ubuntu Terminal.
➋ Write the following command line in your terminal window.
echo $USER
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:
nano env_var_script.sh
- nano: Opens a file in the Nano text editor.
- env_default.sh: Name of the file.
❸ Copy the script mentioned below:
#!/bin/bash
# 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!"
#!/bin/bash is called a shebang or hashbang. It specifies the interpreter that will be used to execute the script. Then it utilizes the echo command to print a message indicating the home directory of the current user, where $HOME represents the value of the HOME environment variable. Subsequently, it uses the $USER environment variable in another echo statement to provide a personalized welcome message 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
- chmod: changes the permissions of files and directories.
- u+x: Here, u refers to the “user” or the owner of the file and +x specifies the permission being added, in this case, the “execute” permission. When u+x is added to the file permissions, it grants the user (owner) of the file permission to execute (run) the file.
- env_default.sh: is the name of the file.
❻ Run the script by the following command:
./env_var_script.sh
Upon execution, env_var_script returns /home/miran as the home directory and also miran as the user name.
Assignment Task
- Write a bash script to display the current user of the system. Use the environment variable to read the task in bash.
- Write a bash script that prompts the user to enter their name, email, and favorite programming language. Store each of these inputs in separate environment variables and display a summary message using those variables.
- Create a bash script that logs each time a user logs into the system. Use environment variables to store the user’s login name, timestamp, and IP address. Append this information to a log file with each login event.
Conclusion
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
Related Articles
- 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]
<< Go Back to Variable Declaration and Assignment | Bash Variables | Bash Scripting Tutorial