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 Check if a Header Is Available in a Linux File – A Complete Guide

   How to Check if a Header Is Available in a Linux File – A Complete Guide When working in Linux environments, developers and system administrators often need to verify whether a specific header , field name, or column exists inside a file. This is especially common when dealing with CSV files , log files , configuration files , or any structured data. This guide explains multiple methods to check whether a header is present using simple Linux command-line tools. Why Check for a Header in Linux? Checking for a header is useful when you want to: Validate data files Ensure correct file formats Prevent script failures Perform conditional processing Linux provides multiple commands to check headers efficiently. 1. Using grep (Simple & Fast) grep -q "HeaderName" filename && echo "Header exists" || echo "Header not found" 2. Check Only the First Line head -n 1 filename | grep -q "HeaderName" 3. Using awk for Ex...

Linux vs Windows: Key Differences for Developers (Complete Guide)

Here we can choosing the right operating system is one of the most important decisions for developers. Linux vs Windows is a long-standing debate, especially for programmers working in web development, DevOps, cloud computing, and data engineering. While Windows offers a user-friendly desktop experience, Linux dominates servers, containers, and cloud platforms. In this guide, we’ll compare Linux vs Windows for developers , covering performance, security, development tools, server usage, and real-world use cases. Linux for Developers Native support for: Docker Kubernetes Git SSH Python, Java, Node.js, Go Terminal-first workflow Same OS used in production servers Windows for Developers Strong support for: .NET & C# Visual Studio Enterprise desktop apps WSL (Windows Subsystem for Linux) improves Linux compatibility Still less native than real Linux Software Installation & Package Management Linux Package managers like: ...

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...