X
Collapse
-
How to Set and Use Environment Variables In Bash Script
In bash scripting, environment variables are used to save and manage the data to modify the processes in the system. Using the bash scripts, you can use the environment variables to configure the system process. Environment variables also offer security and transmit the information from one script to another.
Hence, there are multiple uses of environment variables that you can use to enhance the automation in the Linux system. So, if you want to learn the simple ways to set and use environment variables in a bash script, this tutorial is for you. Here, we have included various examples and use cases of environment variables in bash.
Basics of Setting Up Environment Variable
You directly set the environment variable from the terminal through the export command. For example, let’s create an environment variable “NOTE” having a special text “Welcome to Ubuntu Machine”:
export NOTE="Welcome to Ubuntu Machine"
Once you are done, it is time to use the “NOTE” variable in the script:
#!/bin/bash
echo $NOTE
Now, let’s execute the script and print the result:
./intro.sh
In case you want to create an environment variable in the script, and here is an example to do it:
#!/bin/bash
export UPDATE="sudo apt update"
echo $NOTE
$UPDATE
Now, you can run the above script to get the desired results:
Similarly, you can create a temporary variable that will only work with the single script execution. For example, let’s create two environment variables: WELCOME and UPGRADE:
Now, you can declare the variable while executing the script:
#!/bin/bash
echo $WELCOME
Let's upgrade your system
$UPGRADE
Now, you can declare the variable while executing the script:
WELCOME="Welcome $USER" UPGRADE="sudo apt update && apt upgrade ./script.sh
You can access the environment variables directly from the terminal using the following command:
echo $<environment_variable>
Moreover, you can check all the environment variables available in your system:
printenv
Advanced Approaches to Set Environment
If you want to create an environment variable that should be available after the script execution or the terminal session, then please use the below command:
echo 'export VAR="information"' >> ~/.bashrc
source ~/.bashrc
The above command will add the export to the .bashrc file.
Wrapping Up
So, this was all about the ways to set and use the environment variables in the bash script. We have included various types of methods to set up the environment variables and also use them after the specific terminal session.
Moreover, we recommend you explore your skills and create unique environment variables to automate the tasks using the bash scripting. If you encounter some issues while using the environment variable, then you can check out our guide to resolve the problems with the environment variable.
More...Tags: None
Posting comments is disabled.
Categories
Collapse
Article Tags
Collapse
There are no tags yet.
Latest Articles
Collapse
-
by Kasimba
by George Whittaker
Introduction
In an increasingly interconnected digital world, web applications are the backbone of online services. With this ubiquity comes a significant risk: web applications are prime targets for cyberattacks. Ensuring their security is not just an option but a necessity. Linux, known for its robustness and adaptability, offers a perfect platform for deploying secure web applications....-
Channel: Articles
11-29-2024, 05:12 PM -
-
by Kasimba
by George Whittaker
Introduction
Quantum computing, a revolutionary paradigm, promises to solve problems that are computationally infeasible for classical systems. By leveraging the peculiar principles of quantum mechanics—superposition, entanglement, and quantum interference—quantum computing has emerged as a transformative force across industries. From cryptography and drug discovery to optimization and...-
Channel: Articles
11-27-2024, 06:22 PM -
-
by Kasimba
by George Whittaker
Introduction
Qualitative data analysis (QDA) is a cornerstone of research across various fields, from social sciences to marketing. It involves uncovering patterns, themes, and meanings within non-numerical data such as interviews, focus groups, and textual narratives. In this era of digital tools, MAXQDA stands out as a premier software solution for QDA, empowering researchers to organize...-
Channel: Articles
11-21-2024, 11:31 PM -
-
by Kasimba
by german.suarez
Introduction
In today’s fast-paced digital landscape, ensuring the availability and performance of applications is paramount. Modern infrastructures require robust solutions to distribute traffic efficiently and maintain service availability even in the face of server failures. Enter HAProxy, the de facto standard for high-performance load balancing and failover.
This article...-
Channel: Articles
11-21-2024, 03:00 PM -
-
by KasimbaArch Linux hasn't had a license for any package sources (such as PKGBUILD files) in the past, which is potentially problematic. Providing a license will preempt that uncertainty.
In RFC 40 we agreed to change all package sources to be licensed under the very liberal 0BSD license. This change will not limit what you can do with package sources. Check out the RFC for more on the rationale and prior discussion.
Before we make this change, we will provide contributors with...-
Channel: Articles
11-19-2024, 09:21 AM -
-
by Kasimba
by George Whittaker
Introduction
In the world of cybersecurity and software development, binary analysis holds a unique place. It is the art of examining compiled programs to understand their functionality, identify vulnerabilities, or debug issues—without access to the original source code. For Linux, which dominates servers, embedded systems, and even personal computing, the skill of binary analysis is...-
Channel: Articles
11-18-2024, 07:10 PM -