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 KasimbaI don’t like my prompt, i want to change it. it has my username and host, but the formatting is not what i want. This blog will get you started quickly on doing exactly that.
This is my current prompt below:
To change the prompt you will update .bashrc and set the PS1 environment variable to a new value.
Here is a cheatsheet of the prompt options:
You can use these placeholders for customization:
\u – Username
...-
Channel: Articles
Yesterday, 01:15 AM -
-
by KasimbaIn this post I will show you how to install the ZSH shell on Rocky Linux. ZSH is an alternate shell that some people prefer instead of BASH shell. Some people say ZSH has better auto-completion, theme support, and plugin system. If you want to give ZSH a try its quite easy to install and give it a try. This post is focused on the Rocky Linux user and how to install ZSH and get started with its usage.
Before installing anything new, it’s good practice to update your system packages:...-
Channel: Articles
12-25-2024, 02:01 AM -
-
by Kasimba
by George Whittaker
Introduction
In the world of operating systems, Windows has long held the lion’s share of the market. Its user-friendly interface and wide compatibility have made it the default choice for many. However, in recent years, Linux has steadily gained traction, challenging the status quo with its unique offerings. What was once considered the domain of tech enthusiasts and developers is now...-
Channel: Articles
12-21-2024, 06:52 AM -
-
by KasimbaOverview of NGINX and Apache
NGINX and Apache are leading web server solutions utilized for hosting websites and web applications. Apache, developed by the Apache Software Foundation, offers robust configuration options and extensibility. NGINX, created by Igor Sysoev, is known for its efficiency in handling numerous concurrent connections with low resource utilization. Both servers function not only as HTTP servers but also as reverse proxies, load balancers, and more.
What is
...-
Channel: Articles
12-21-2024, 03:54 AM -
-
by KasimbaHi everyone, I hope you are enjoying the BETA so far! This release introduces new features, tools, and artwork, so we anticipate a good number of bug reports. Every single fix helps us refine and improve the final release. Your feedback during the BETA phase is extremely important to us. Linux Mint 22.1 is our […]
More...-
Channel: Articles
12-16-2024, 11:50 AM -
-
by Kasimba
by George Whittaker
Introduction
Remote access is a cornerstone of modern IT infrastructure, enabling administrators and users to manage systems, applications, and data from virtually anywhere. However, with great power comes great responsibility—ensuring that remote access remains secure is paramount. This is where OpenSSH steps in, providing robust, encrypted communication for secure remote management....-
Channel: Articles
12-13-2024, 10:31 PM -