Menu

Log in

Sign up

From beginner to master of web design, coding, infrastructure operation, business development and marketing

  • COURSES
  • HTML & CSS Introduction
  • HTML & CSS Coding with AI
  • Linux Introduction
  • Docker Basics
  • Git & GitHub Introduction
  • JavaScript Coding with AI
  • Django Introduction
  • AWS Basics
  • Figma Introduction
  • SEO Tutorial for Beginners
  • SEO with AI
  • OTHERS
  • About
  • Terms of Service
  • Privacy Policy

© 2024 D-Libro. All Rights Reserved

Linux IntroductionChapter 5. Redirection, Pipe and Shell Script

Shell Variable and Environmental Variable

Shell Variable and Environmental Variable

Shell and Environmental Variables in Linux

There are two types of variables in Linux – shell variables and environmental variables. Shell variables are only defined for the current shell and the variables cannot be used in other shells or processes. On the other hand, environmental variables are variables that are also defined for other shells or processes, and not just the current one. The environmental variables are inherited by any child shells or processes.

Declaration of variables

The ways to declare shell variables and environmental variables are different.

Shell variable: connect a variable and an assigned string with =. For example, abc=2023.

Shell variable

Environmental variable: use the export command and connect a variable and an assigned string with =. For example, export ABC=1988.

Environmental variable with the export command

Inheritance of variables

As explained above, the key difference occurs if the variables are inherited in child processes. To see the difference, create two shell scripts.

First, create a shell script for the parent process.

Command Line - INPUT
vim variable_parent.sh

In the variable_parent.sh file, declare two types of variables.

  • abc : a shell variable
  • ABC : an environmental variable

In the parent shell script below, we’ll add a command to run a child script (variable_child.sh).

variable_parent.sh
#!/bin/bash
  
#shell variable
abc=2023

#environmental variable
export ABC=1988

echo "A shell variable in the parent shell script"
echo abc=$abc
echo "An environmental variable in the parent shell script"
echo ABC=$ABC

#run child script
./variable_child.sh

Next, create a shell script for the child process.

Command Line - INPUT
vim variable_child.sh

In the variable_child.sh file, add the echo commands to call the two variables declared in the parent script.

variable_child.sh
#!/bin/bash
  
echo "A shell variable in the child script"
echo abc=$abc
echo "An environmental variable in the child script"
echo ABC=$ABC

Add execution permissions to these files and execute the parent shell script.

Command Line - INPUT
sudo chmod u+x variable_parent.sh
sudo chmod u+x variable_child.sh
./variable_parent.sh

You can see that the values (2023 and 1988) assigned to both variables (abc and ABC) in the parent shell script are properly returned; however, the value assigned to abc (shell variable) in the child script is blank, while the value assigned to ABC (environmental variable) is properly returned.

Command Line - RESPONSE
A shell variable in the parent shell script
abc=2023
An environmental variable in the parent shell script
ABC=1988
A shell variable in the child script
abc=
An environmental variable in the child script
ABC=1988

From this example, you can see that shell variables are only working within the same process, while environmental variables are inherited in child processes.


You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Launching Apache Web Server on Linux

Launch Apache Web Server

Adjusting Social Login for Django Production

Social Login for Production

Creating User Groups in Linux

groupadd (Add Group)

Setting Up Social Login with Google via Django Allauth

Django Allauth (6) – Social Login with Google

Basics of Django App Deployment

Overview of Django App Deployment (1)

Launching Apache Web Server on Linux

Launch Apache Web Server

Adjusting Social Login for Django Production

Social Login for Production

Creating User Groups in Linux

groupadd (Add Group)

Setting Up Social Login with Google via Django Allauth

Django Allauth (6) – Social Login with Google

Basics of Django App Deployment

Overview of Django App Deployment (1)

Tags:

Linux Command

Variable

Shell Variable

Environmental Variable

Linux Introduction
Course Content

Chapter 1. Linux Basics

What Is OS?

CUI and GUI

Linux Distributions

Package Manager

Kernel and Shell

Current Working Directory

Linux Directory Structure

Absolute Path and Relative Path (Linux OS)

Linux Command Syntax

Special Characters and Escape Character

Chapter 2. Linux Key Commands

Setting Up Linux Environment on AWS

pwd (Print Working Directory)

cd (Change Directory)

ls (List Contents of Directory)

mkdir (Make Directory)

rmdir (Remove Directory)

touch (Create File)

rm (Remove File)

mv (Move File and Directory)

cp (Copy File and Directory)

cat (Display File Content)

sort (Sort File Contents)

grep (Global Regular Expression Print)

Regular Expression

find (Find File and Directory)

Wildcard

ln (Create Link to File and Directory)

Chapter 3. Vim Editor

What Is Vim and How to Launch It?

Normal, Insert and Visual Mode

Normal Mode (1) – Move Cursor

Normal Mode (2) – Delete

Normal Mode (3) – Copy and Paste

Normal Mode (4) – Undo and Redo

Normal Mode (5) – Search Phrase

Normal Mode (6) – Replace Phrase

Normal Mode (7) – Save and Exit

Insert Mode

Visual Mode

Chapter 4. User, Group and Permission

What Are User, Group And Permission in Linux?

Permission (Access Mode) by Owner Status

Superuser (Root User) vs. Normal User

sudo (Run Command with Superuser Privileges)

su (Switch User)

useradd (Add User)

passwd (Set Password)

userdel (Delete User)

Group – Primary Group and Secondary Group

groupadd (Add Group)

usermod (Modify User Account Information)

gpasswd (Add and Delete Users to Group)

groupdel (Delete Group)

chown (Change Owner of File and Directory)

chgrp (Change Group of File and Directory)

chmod (Change Access Mode)

chmod Command with Numbers

w and who (Check Current User Login Status)

id and groups (Check User ID and Group)

getent (Display User and Group Data)

Chapter 5. Redirection, Pipe and Shell Script

Standard Input Output and Redirection

Pipe (Combine Commands)

less (Display Content with Pager)

tr (Replace Characters)

cut (Extract Data Sections)

uniq (Extract Unique Data Lines)

Shell Script

echo (Echo input)

read (Read and Store Input)

Shell Variable and Environmental Variable

source (Execute Shell Script and Refresh Environmental Variables)

Chapter 6. Linux Commands for Command Management

history (Check Command History)

alias (Create Command Shortcuts)

man (Display Manual)

type, which and whereis (Display Command Information)

Package Manager Command

tree (Display Directory Tree)

Chapter 7. SSH Remote Connection

SSH (Secure Shell)

Locate .ssh Directory

SSH Remote Login (1) – Use Key Pair Generated by Server

SSH Remote Login (2) – Use Key Pair Generated by Client

SSH Config File

SSH Remote Login with Visual Studio Code

SCP (Secure Copy Protocol)

SCP with SSH Config File

SFTP (Secure File Transfer Protocol)

Other File Transfer Commands

Chapter 8. Linux Process Management

Process and Job

Foreground and Background Jobs

jobs and ps (Display Jobs and Processes)

Signals

Create, Stop and Terminate Jobs

Daemon Processes

What Is Service on Linux?

Systemd

Unit File

Systemctl Sub-Commands

Create Custom Unit and Start at Boot

Firewall

UFW (Uncomplicated Firewall)

Web Server

Launch Apache Web Server

Chapter 1. Linux Basics

What Is OS?

CUI and GUI

Linux Distributions

Package Manager

Kernel and Shell

Current Working Directory

Linux Directory Structure

Absolute Path and Relative Path (Linux OS)

Linux Command Syntax

Special Characters and Escape Character

Chapter 2. Linux Key Commands

Setting Up Linux Environment on AWS

pwd (Print Working Directory)

cd (Change Directory)

ls (List Contents of Directory)

mkdir (Make Directory)

rmdir (Remove Directory)

touch (Create File)

rm (Remove File)

mv (Move File and Directory)

cp (Copy File and Directory)

cat (Display File Content)

sort (Sort File Contents)

grep (Global Regular Expression Print)

Regular Expression

find (Find File and Directory)

Wildcard

ln (Create Link to File and Directory)

Chapter 3. Vim Editor

What Is Vim and How to Launch It?

Normal, Insert and Visual Mode

Normal Mode (1) – Move Cursor

Normal Mode (2) – Delete

Normal Mode (3) – Copy and Paste

Normal Mode (4) – Undo and Redo

Normal Mode (5) – Search Phrase

Normal Mode (6) – Replace Phrase

Normal Mode (7) – Save and Exit

Insert Mode

Visual Mode

Chapter 4. User, Group and Permission

What Are User, Group And Permission in Linux?

Permission (Access Mode) by Owner Status

Superuser (Root User) vs. Normal User

sudo (Run Command with Superuser Privileges)

su (Switch User)

useradd (Add User)

passwd (Set Password)

userdel (Delete User)

Group – Primary Group and Secondary Group

groupadd (Add Group)

usermod (Modify User Account Information)

gpasswd (Add and Delete Users to Group)

groupdel (Delete Group)

chown (Change Owner of File and Directory)

chgrp (Change Group of File and Directory)

chmod (Change Access Mode)

chmod Command with Numbers

w and who (Check Current User Login Status)

id and groups (Check User ID and Group)

getent (Display User and Group Data)

Chapter 5. Redirection, Pipe and Shell Script

Standard Input Output and Redirection

Pipe (Combine Commands)

less (Display Content with Pager)

tr (Replace Characters)

cut (Extract Data Sections)

uniq (Extract Unique Data Lines)

Shell Script

echo (Echo input)

read (Read and Store Input)

Shell Variable and Environmental Variable

source (Execute Shell Script and Refresh Environmental Variables)

Chapter 6. Linux Commands for Command Management

history (Check Command History)

alias (Create Command Shortcuts)

man (Display Manual)

type, which and whereis (Display Command Information)

Package Manager Command

tree (Display Directory Tree)

Chapter 7. SSH Remote Connection

SSH (Secure Shell)

Locate .ssh Directory

SSH Remote Login (1) – Use Key Pair Generated by Server

SSH Remote Login (2) – Use Key Pair Generated by Client

SSH Config File

SSH Remote Login with Visual Studio Code

SCP (Secure Copy Protocol)

SCP with SSH Config File

SFTP (Secure File Transfer Protocol)

Other File Transfer Commands

Chapter 8. Linux Process Management

Process and Job

Foreground and Background Jobs

jobs and ps (Display Jobs and Processes)

Signals

Create, Stop and Terminate Jobs

Daemon Processes

What Is Service on Linux?

Systemd

Unit File

Systemctl Sub-Commands

Create Custom Unit and Start at Boot

Firewall

UFW (Uncomplicated Firewall)

Web Server

Launch Apache Web Server