• Contact
  • About Me
  • Privacy Policy
  • Disclaimer
DefenceDev
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
    • Bash Scripts
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
  • Web Tools
No Result
View All Result
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
    • Bash Scripts
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
  • Web Tools
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
September 11, 2025
in Bash Scripting Tutorials
0
Lesson 2: Linux Bash Variables

Lesson 2: Linux Bash Variables

0
SHARES
9
VIEWS
Share on FacebookShare on Twitter
Table of Contents
  • Lesson 2: Linux Bash Variables
  • What are Linux Bash Variables?
  • Defining Variables
  • Accessing Variables
  • Types of Variables
    • Common Environment Variables
    • List and Read Environment Variables
    • Setting Environment Variables
    • Accessing Environment Variables
  • Persisting Environment Variables
  • Special Variables

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:

  • 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:

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.

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:

ADVERTISEMENT
source ~/.bash_profile

Access to the defined variable:

ADVERTISEMENT
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

ADVERTISEMENT
Previous Post

Lesson 1: Introduction to Bash Scripting

Next Post

Lesson 3: Bash Control Structures – if else

neo

neo

Related Posts

Lesson 11: Practical Linux Bash Examples and Projects
Bash Scripting Tutorials

Lesson 11: Practical Linux Bash Examples and Projects

Lesson 10: Bash File Operation (Read, Write, Copy)
Bash Scripting Tutorials

Lesson 10: Bash File Operation (Read, Write, Copy)

Lesson 9: Bash Maths Operations (Bash Arithmetic) Examples
Bash Scripting Tutorials

Lesson 9: Bash Maths Operations (Bash Arithmetic) Examples

Lesson 8: Working with Bash Strings
Bash Scripting Tutorials

Lesson 8: Working with Bash Strings

Lesson 7: Bash Functions with Examples
Bash Scripting Tutorials

Lesson 7: Bash Functions with Examples

Lesson 6: Using Bash Command Substitution
Bash Scripting Tutorials

Lesson 6: Using Bash Command Substitution

Next Post
Lesson 3: Bash Control Structures - if else

Lesson 3: Bash Control Structures - if else

Recommended

Fortinet FortiGate Policy Implementation

Fortinet FortiGate Policy Implementation

Nextcloud: Manual Upgrade from 25.0 to 26.0 on Ubuntu 22.04

Nextcloud Manual Upgrade on Ubuntu 22.04: Step-by-Step Guide

View & Copy Image Metadata Online – Camera & GPS Info

View & Copy Image Metadata Online – Camera & GPS Info

Online WebP to JPG Converter

Online WebP to JPG Converter

Image Metadata Remover – Clean EXIF and GPS Info Securely

Image Metadata Remover – Clean EXIF and GPS Info Online

Categories

  • 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
  • Web Tools
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

Recent News

View & Copy Image Metadata Online – Camera & GPS Info

View & Copy Image Metadata Online – Camera & GPS Info

Online WebP to JPG Converter

Online WebP to JPG Converter

  • 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
    • Bash Scripts
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
  • Web Tools

© 2025 defencedev.com - All rights reserved.