Linux Variable Exporting

LinuxLinuxBeginner
Practice Now

Introduction

In a distant, bustling corner of our galaxy, there exists an elite team of superhero coders, known as the Code Guardians. They are masters of various programming languages, shell scripting, and system administration, traveling across star systems to protect the integrity of code and ensure stability in the cyber-infrastructure.

One of these Code Guardians, a renowned superhero named Varon, specializes in the ancient art of environment manipulations on Linux-based systems. Varon is currently on a mission to teach budding intergalactic developers like you the power of exporting variables in a Linux environment.

Your objective, should you choose to accept it, is to master the export command and learn how to effectively manage environment variables to communicate between processes and scripts, ensuring your applications run harmoniously across the galaxies.

Let the cosmic adventure begin!


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux/UserandGroupManagementGroup -.-> linux/export("`Variable Exporting`") subgraph Lab Skills linux/export -.-> lab-271279{{"`Linux Variable Exporting`"}} end

Understanding and Setting Environment Variables

In this step, you'll learn the basics of setting environment variables in a Linux environment. We'll start by creating a simple variable and then proceed to make it an environment variable using the export command.

Start by opening the terminal in your /home/labex/project location and create a script named greet.sh in the ~/project directory with the following content:

Create the file:

touch ~/project/greet.sh

Add the content:

#!/bin/bash
## This script greets the user

name="Galactic Dev"
echo "Welcome aboard, $name!"

Mark the script as executable:

chmod +x ~/project/greet.sh

Run the script using:

~/project/greet.sh

You should see the output:

Welcome aboard, Galactic Dev!

Now, modify the script to use an environment variable:

#!/bin/bash
## This script greets the user with an environment variable

echo "Welcome aboard, $GALACTIC_NAME!"

Before running the script again, export the GALACTIC_NAME variable in your current shell:

export GALACTIC_NAME="Intergalactic Developer"

Now, when you run the greet.sh script, you'll have a different output utilizing the exported variable:

~/project/greet.sh

The output should be:

Welcome aboard, Intergalactic Developer!

Summary

In this lab, we took on the exciting task of exploring the vast cosmos of Linux and the powerful export command. We've followed the footsteps of Varon, our intergalactic super coder, to understand the significance of setting and exporting environment variables to ensure seamless communication between processes. We've learned how to set temporary environment variables, make them persistent across sessions, and create scripts that leverage these variables to create a more dynamic and adaptable Linux environment.

Your understanding of environment variables should now be out of this world, and you are ready to tackle challenges that span the entire galaxy of Linux systems. With these skills, you can proudly stand among the stars as a true Code Guardian.

Other Linux Tutorials you may like