• Contact
  • About Me
  • Privacy Policy
DefenceDev
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
No Result
View All Result
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
No Result
View All Result
DefenceDev
No Result
View All Result
ADVERTISEMENT
Home Linux Tutorials Bash Scripting Tutorials

Lesson 2: Linux Bash Variables

neo by neo
May 27, 2025
in Bash Scripting Tutorials
0
Lesson 2: Linux Bash Variables

Lesson 2: Linux Bash Variables

0
SHARES
5
VIEWS
Share on FacebookShare on Twitter
ADVERTISEMENT

Lesson 2: Linux Bash Variables

Welcome to Lesson 2: Linux Bash Variables. In this lesson, we will dive into the fundamentals of variables in Bash scripting, an essential concept for effective script writing and automation. You’ll learn how to define variables, work with different types of data, and use variables to store and manipulate values. By the end of this lesson, you’ll have the tools you need to make your Bash scripts more dynamic and powerful. Let’s get started with understanding and using Bash variables!

What are Linux Bash Variables?

Variables in Linux Bash are used to store data that can be referenced and manipulated throughout your script. They are essential for creating dynamic and flexible scripts.

Defining Variables

To define a variable in Bash, you simply assign a value to a name. Here’s the basic syntax:

variable_name=value

Example:

greeting="Hello, World!"

Accessing Variables

To access the value of a variable, you use the $ symbol followed by the variable name.

echo $greeting

This will output:

Hello, World!

Types of Variables

  1. Local Variables: These are defined within a function and are only accessible within that function.
  2. Global Variables: These are defined outside of all functions and are accessible from anywhere in the script.
  3. Environment Variables: These are global variables that are available to any child process of the shell.

Common Environment Variables

Some commonly used environment variables include:

  • PATH: Specifies the directories in which the shell looks for executable files.
  • HOME: Indicates the home directory of the current user.
  • USER: Contains the name of the current user.
  • SHELL: Specifies the path to the current shell.
  • LANG: Defines the language and locale settings.
  • PWD: Represents the current working directory.

List and Read Environment Variables

You can list all environment variables using commands like printenv, env, or set:

ADVERTISEMENT
  • printenv: Displays all or specific environment variables.
  • env: Shows all environment variables.
  • set: Lists all shell variables, including environment variables.
Displays all environment variables
Displays all Environment variables

To access to the envoronment variables you can do the following:

ADVERTISEMENT
echo $SHELL

This will output:

/bin/bash

Setting Environment Variables

You can set environment variables in the shell using the export command. For example:

export MY_VARIABLE="some_value"

This sets MY_VARIABLE to “some_value” for the current session.

ADVERTISEMENT

Accessing Environment Variables

To access the value of an environment variable, you use the $ symbol followed by the variable name. For example:

echo $MY_VARIABLE

Persisting Environment Variables

To make environment variables persistent across sessions, add them to your shell’s configuration file (e.g., .bashrc or .bash_profile).
Example:

 nano .bashrc
# Add the variable at the end of file
GREETING="Hello, World!"

Reload the profile:

source ~/.bash_profile

Access to the defined variable:

echo $GREETING

Output:

Hello, World!

Special Variables

Bash also has several special variables that are predefined and have specific meanings:

  • $0: The name of the script.
  • $1, $2, ...: The positional parameters.
  • $#: The number of positional parameters.
  • $@: All the positional parameters.
  • $$: The process ID of the current shell.
  • $?: The exit status of the last command executed.

defencedev.com


«Previous: Lesson 1: Introduction to Bash Scripting
Next: Lesson 3: Bash Control Structures – if else

About The Author

neo

See author's posts

Tags: bash-scripting
ADVERTISEMENT
Previous Post

Lesson 1: Introduction to Bash Scripting

Next Post

Lesson 3: Bash Control Structures – if else

neo

neo

Next Post
Lesson 3: Bash Control Structures - if else

Lesson 3: Bash Control Structures - if else

Follow Us

  • Trending
  • Comments
  • Latest
MikroTik: Export Configuration in Text File

MikroTik: Export Configuration in Text File

Fortinet FortiGate: Static Route Configuration via GUI and CLI

Fortinet FortiGate: Static Route Configuration via GUI and CLI

Immich: Installation on Ubuntu 22.04

Immich: Installation on Ubuntu 22.04

Fortinet FortiGate Upgrade Path Tool

Fortinet FortiGate Upgrade Path Tool

NextCloud: Port Forwarding - Essential Ports for Smooth Functionality

NextCloud: Port Forwarding – Essential Ports for Smooth Functionality and Remote Access

Organizing and Managing Photos with Immich: Features I Use

Organizing and Managing Photos with Immich: Features I Use

Install Ubuntu 22.04 on Oracle VM Virtual Box

Install Ubuntu 22.04 on Oracle VM Virtual Box

Linux Directories: cd (Change Directory), pwd (Print Working Directory), mkdir (Make Directory)

Linux Directories: cd (Change Directory), pwd (Print Working Directory), mkdir (Make Directory)

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

Recent News

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

MikroTik: Check Your Wireless Password

MikroTik: Check Your Wireless Password

ADVERTISEMENT

DefenceDev Tutorials

defencedev Logo

Whether you’re just starting or looking to expand your skills, I hope you find useful information and engaging discussions here. Let me take you through my journey and the goals behind this space!

Follow Us

Browse by Category

  • Blog
  • Cloud
    • Private
  • Linux Tutorials
    • Bash Scripting Tutorials
    • Commands
    • Networking
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Solutions
    • Docker
  • Video Tutorials
    • MikroTik Videos

Recent News

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

  • Site Map
  • Privacy Policy
  • Facebook Page
  • Disclaimer
  • Contact
  • About Me

© 2025 defencedev.com - All rights reserved.

No Result
View All Result
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos

© 2025 defencedev.com - All rights reserved.