Skip to main content

Shell Scripting Variables: 7 Important Discovery Every Beginner Must Know

Shell Scripting Variables Explained: A Beginner’s Guide (Bourne Shell)

Variables are one of the most fundamental concepts in programming, and shell scripting is no exception. In shell scripts, variables allow you to store data in memory, reuse values, and write flexible, maintainable scripts.

In this tutorial, you’ll learn what variables are in the Bourne shell, how to define and use them correctly, common mistakes to avoid, and practical examples you can apply immediately.

Beginner guide to shell scripting variables



What Is a Variable in Shell Scripting?

A variable is a symbolic name that represents a value stored in memory. You can:

  • Assign a value to a variable

  • Read the value later

  • Modify the value during script execution

In shell scripting, variables are untyped, meaning you don’t need to declare their data type.


Why Variables Are Important in Shell Scripts

Variables help you:

  • Avoid repeating values

  • Make scripts dynamic

  • Improve readability

  • Pass data between commands

  • Store user input and command output

Without variables, shell scripts would be rigid and hard to maintain.


Basic Variable Assignment in Bourne Shell

The basic syntax for defining a variable is:

VAR=value

Important Rule

There must be NO spaces around the = sign.

Correct:

NAME=World

Incorrect:

NAME = World

Why spaces break it?

  • VAR=value → Shell treats this as a variable assignment

  • VAR = value → Shell thinks VAR is a command and tries to execute it


Using Variables in a Shell Script

To access the value of a variable, prefix it with $.

Example: Hello World Using Variables

#!/bin/sh

MESSAGE="Hello World"
echo $MESSAGE

Output:

Hello World

Variable Naming Rules in Shell Scripting

Follow these rules to avoid errors:

  • Can contain letters, numbers, and underscores

  • Must start with a letter or underscore

  • Cannot start with a number

  • Cannot contain spaces

Examples

USER_NAME="admin"
count=10
_total=50

Invalid examples:

2name=value
user-name=value
my var=value

Reading Variable Values

You can use variables in commands:

FILE="test.txt"
ls $FILE

You can also combine variables with text:

NAME="Linux"
echo "Welcome to $NAME Shell Scripting"

Reassigning Variables

Shell variables can be changed at any time:

COUNT=5
COUNT=10
echo $COUNT

Output:

10

Using Variables in Commands (Command Substitution)

You can store command output in a variable:

DATE=$(date)
echo "Today is $DATE"

This is very useful for automation scripts.


Environment Variables vs Shell Variables

  • Shell variables exist only in the current shell

  • Environment variables are available to child processes

Example:

export PATH

Environment variables are covered in detail in Variables – Part II.


Common Mistakes with Shell Variables

Adding spaces around =

VAR = value

Forgetting $ when accessing value

echo VAR   # prints VAR, not the value

Using special characters in variable names

my-var=value

point to be noted

  • Use uppercase for constants

  • Use meaningful names

  • Quote variables when in doubt:

    echo "$VAR"
    
  • Initialize variables before using them


Frequently Asked Questions (FAQ)

 Why does VAR = value not work in shell?

Because the shell interprets VAR as a command, not a variable assignment.


Are shell variables case-sensitive?

Yes.

VAR=10
var=20

These are two different variables.


Do I need to declare variables in shell scripts?

No. Shell scripting does not require variable declaration.


Summary

Variables are the backbone of shell scripting. Understanding how to assign, access, and manage variables correctly is essential for writing effective Bourne shell scripts.

Related Topics:

Here are the External tutorial -

Comments

Popular posts from this blog

How to Improve Node.js Performance (100% Working Techniques)

How to Improve Node.js Performance (100% Working Techniques) Optimize Express.js for Speed, Security & SEO Node.js is known for its high performance, but improper configuration can significantly slow down your application. In this article, you’ll learn proven and production-ready techniques to optimize Node.js performance, improve server response time, and boost SEO rankings. Why Node.js Performance Matters for SEO Google ranking heavily depends on: Server Response Time (TTFB) Page Speed Security Headers Reduced Server Load A slow Node.js backend directly affects: SEO ranking User experience Crawl budget 1. Disable x-powered-by Header Default Behavior Express exposes the following header: X-Powered-By: Express This reveals your backend technology and slightly increases response size. Best Practice app.disable('x-powered-by'); Benefits Improves security Reduces header size Prevents fingerprinting Recommended by OWASP 2. Use Weak ETag for Better Performance Problem with Def...

Top 10 Linux File System Basics – A Complete for Beginners

  Top 10 Linux File System Basics -Introduction The Linux file system is the backbone of how Linux operating systems store, organize, and manage data. Whether you are a Linux beginner, system administrator, DevOps engineer, or developer , understanding Linux file system basics is essential for efficient system management and security. we will cover the top 10 Linux file system basics with simple explanations, examples, and real-world use cases. 1. Everything Is a File in Linux One of the most important Linux file system principles is that everything is treated as a file —including: Regular files Directories Devices Processes Examples: /etc/passwd → user data file /dev/sda → disk device /proc/cpuinfo → CPU information This design makes Linux powerful and flexible. 2. Linux Directory Structure (Filesystem Hierarchy) Linux follows a standard directory layout called the Filesystem Hierarchy Standard (FHS) . Key directories: Directory Purpose / Root directory /bin Essential binarie...

Building Multi-Agent Systems: Practical Tutorial for 2026

Building Multi-Agent Systems: Practical Tutorial for 2026 Introduction Multi-Agent Systems (MAS) are becoming one of the most powerful architectures in modern AI. In 2026, they are widely used in automation, trading bots, robotics, distributed AI, smart cities, and enterprise AI systems. Instead of relying on one large AI model, multi-agent systems use multiple intelligent agents that collaborate, compete, or coordinate to solve complex problems. This tutorial explains how to build multi-agent systems from scratch in a practical and beginner-friendly way. What is a Multi-Agent System? A Multi-Agent System (MAS) consists of multiple autonomous AI agents that: Perceive the environment Make decisions independently Communicate with other agents Work toward shared or individual goals Each agent has its own role, memory, and reasoning capability. Experts from IBM, Google Cloud, Gartner, Deloitte, and others are calling 2026 the "year of multi-agent systems" and "multi-agent o...