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 the digital age, securing files and controlling access to them is paramount. File permissions play a crucial role in maintaining the integrity and confidentiality of data. This article delves into the intricacies of file permissions, ownership, and access control, providing a guide to understanding and managing these aspects effectively.
In today's interconnected...-
Channel: Articles
Yesterday, 05:41 PM -
-
by Kasimba
by George Whittaker
In the world of Linux system administration, managing disk usage effectively is crucial to ensuring a stable and efficient environment. One powerful tool for this purpose is the implementation of disk quotas, which helps administrators control the amount of disk space and number of inodes that individual users or groups can use. This article delves into the intricacies of Linux disk quotas, explaining...-
Channel: Articles
05-15-2024, 03:32 PM -
-
by KasimbaIn this article, I will show you how to create a Fedora 40 virtual machine on Proxmox VE.
This article is the foundation of the Proxmox VE Fedora 40 virtual machine GPU passthrough guide. Once you have a Fedora 40 virtual machine set up, you can passthrough a GPU (i.e. NVIDIA GPU) to the Fedora 40 Proxmox VE virtual machine and do amazing things on the virtual machine.
Table of Contents
...-
Channel: Articles
05-14-2024, 09:14 PM -
-
by KasimbaIf you passthrough an NVIDIA GPU to a Fedora 40 Proxmox VE virtual machine, you get many benefits, such as:
- Stream the virtual machine’s display directly on the monitor connected to the NVIDIA GPU.
- Add a USB keyboard and a mouse to the virtual machine and use it like a real desktop computer.
- Get near-native 3D performance on the virtual machine.
In this article, I will show you how to passthrough an NVIDIA GPU to a Fedora 40 Proxmox VE virtual machine and use the virtual machine just...-
Channel: Articles
05-14-2024, 09:14 PM -
by KasimbaSAVE UP TO 50% TODAY *Offer ends May 21, 2024 Learn More at Linux Foundation Training
The post Further Your Education with Courses & Certifications appeared first on Linux.com.
More...-
Channel: Articles
05-14-2024, 12:25 PM -
-
by KasimbaNautilus file manager can share directories/folders of your Ubuntu 24.04 LTS system that you can access from Windows, MacOS, and other Linux devices.
You may not find the file-sharing option in the context menu of the Nautilus file manager in a new Ubuntu 24.04 LTS installation. That’s because the Nautilus file-sharing option is not enabled by default.
In this article, I will show you how to enable the Nautilus Samba/CIFS file-sharing option...-
Channel: Articles
05-13-2024, 03:46 AM -