How to Read Environment Variables in Bash Script? [2 Methods]

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

Read Environmental Variable through Bash Command LineAs 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
EXPLANATION
  • 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!"
EXPLANATION

#!/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
EXPLANATION
  • 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

Read Environment Variables in Bash ScriptsUpon execution, env_var_script returns /home/miran as the home directory and also miran as the user name.

Assignment Task

  1. Write a bash script to display the current user of the system. Use the environment variable to read the task in bash.
  2. 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.
  3. 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

How to check environment variables exist in bash?
Use the echo command to check if an environment variable exists in Bash. The syntax that should follow is echo $VARIABLE_NAME. Replace VARIABLE_NAME with the name of the environment variable you want to check. If the variable exists, its value will be printed; otherwise, it will display a blank line.

How to access environment variables in a shell script?
In a shell script, you can access environment variables using the following syntax:

$VARIABLE_NAME. Replace VARIABLE_NAME with the name of the environment variable you want to access. The $ symbol followed by the variable name will provide the value of the environment variable within the shell script.

Are environment variables visible?
Yes, environment variables are visible to any process running on the system. They can be accessed and modified by programs and scripts. However, it’s important to be cautious when storing sensitive information in environment variables, as they can potentially be exposed to other users or processes on the same machine.
How do I use an env file?
To use an env file in a shell script or during development, first create a .env file with VARIABLE_NAME=variable_value entries. Then source the file in your script: source /path/to/.env. Next access the environment variables using $VARIABLE_NAME.

Related Articles


<< Go Back to Variable Declaration and Assignment | Bash Variables | Bash Scripting Tutorial

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Mohammad Shah Miran

Hey, I'm Mohammad Shah Miran, previously worked as a VBA and Excel Content Developer at SOFTEKO, and for now working as a Linux Content Developer Executive in LinuxSimply Project. I completed my graduation from Bangladesh University of Engineering and Technology (BUET). As a part of my job, i communicate with Linux operating system, without letting the GUI to intervene and try to pass it to our audience.

Leave a Comment