Shell Scripting Shell Variables (original) (raw)

Last Updated : 7 May, 2026

A shell variable is a named container used to store data in memory while a shell script is running. Think of it as a labelled box that holds data during script execution. The stored value can be a string, number, filename or even the output of a command. Variables make shell scripts dynamic and flexible instead of hard-coded and static.

Types of Shell Variables

Shell variables can be categorized based on their scope, availability and purpose.

1. Local Variables

Local variables are variables that are defined inside a shell or script and exist only for the duration of that shell session or script execution. Once the script finishes or the shell exits, these variables are automatically destroyed.

2. Environment Variables

Environment variables are special variables that are stored in the shell environment and are automatically passed to child processes created from that shell. They are used to configure system behavior and application settings.

**Examples: PATH, HOME, USER, PWD

3. Built-in Shell Variables

Built-in shell variables are predefined variables that are automatically created and managed by the shell to provide information about the system, shell environment and script execution state.

**Examples:

Rules for Defining Variables

When creating shell variables, you must follow certain rules to ensure your script runs correctly. Incorrect definitions can cause errors or unexpected behavior.

1. No Spaces Around =

The equals sign (=) must not have spaces on either side.

VAR="Hello"

**Note: Shell treats spaces as command separators, so spaces around = break the assignment.

2. Use the Assignment Operator =

**Example:

name="John"

3. Variable Naming Rules

**Conventions:

4. Case Sensitivity

Accessing Variables

Once a variable is defined, you can use its value in a script by prefixing the variable name with $. This tells the shell to replace the variable name with its stored value.

1. Basic Access

Display or use the value of a variable.

**Script:

#!/bin/bash

VAR_1="Devil"
VAR_2="OWL"

echo "$VAR_1$VAR_2"

**Output:

Accessing variable

Example of Accessing variable

2. Read only Variables.

These variables are read only i.e., their values could not be modified later in the script. Following is an example:

#!/bin/bash
var1="Devil"
var2=23
readonly var1
echo var1var1 var1var2

var1=23
echo var1var1 var1var2

**Output:

Read only Variables.

Example of Read only Variables.

3. Unsetting Variables

Remove a variable from memory.

#!/bin/bash

var1="Devil"
var2=23
echo var1var1 var1var2

unset var1

echo var1var1 var1var2

**Output:

Unsetting Variables

Example of Unsetting Variables

**Note: The unset command cannot be used to unset read-only variables.

Scenario-Based Examples

1. Store User Input and Calculate Area

Use variables to take input from the user and perform calculations.

#!/bin/bash

echo "Enter the length of the rectangle:"
read length

echo "Enter the width of the rectangle:"
read width

area=$((length * width))

echo "The area of the rectangle is: $area"

Giving input to variables

Giving input to the variable

2. Display Message Based on Time

Store command output in a variable and use it to control logic.

#!/bin/bash

time=$(date +%H)

if [ $time -lt 12 ]; then
message="Good Morning User"
elif [ $time -lt 18 ]; then
message="Good Afternoon User"
else
message="Good Evening User"
fi

echo "$message"

To Store and Display Message by variables

Getting output based on the time of the day.

3. Combined Shell Script Example

Demonstrates all types of shell variables including local, read-only and unset together in one script.

#!/bin/bash

Variable definitions (local)

Var_name="Devil"
Var_age=23

Accessing variables using $

echo "Name is VarnameandageisVar_name and age is VarnameandageisVar_age."

Read-only variable

var_blood_group="O-"
readonly var_blood_group
echo "Blood group is $var_blood_group and read-only."
echo "Error occurs if trying to modify a read-only variable."

Attempt to modify read-only variable

var_blood_group="B+"
echo

Unsetting a variable

unset Var_age
echo "After unsetting Var_age..."
echo
echo "Name is Varname,bloodgroupisVar_name, blood group is Varname,bloodgroupisvar_blood_group and age is $Var_age..."

**Output:

All outputs

Shell and Its Type

A shell is a program that provides a user interface to interact with the operating system. It allows users to execute commands, run scripts and manage system resources. Essentially, the shell is the environment where scripts and programs are executed.

There are several types of shells available, each with its own features and syntax:

Uses of Shell Variables in Shell Scripting

Shell variables are fundamental to shell scripting because they allow scripts to store, manipulate and pass data dynamically. They make scripts flexible, interactive and capable of handling real-time data. Their main uses include:

Quick Reference for Shell Variables