USD ($)
$
United States Dollar
Euro Member Countries
India Rupee

Variables and Data Types

Lesson 33/49 | Study Time: 20 Min

Variables are fundamental components of shell scripting that allow scripts to store, manipulate, and retrieve data dynamically. Unlike some programming languages, shell scripting in Linux does not enforce strict data types, making variables versatile and straightforward to use. Understanding how to declare, access, and use variables effectively is essential for writing flexible and powerful scripts that automate system tasks.

What are Variables?

A variable is a symbolic name assigned to a value or data. They enable scripts to hold data such as numbers, text strings, or the output of commands. Variables can be local to a script, global within a shell session, or environment variables inherited by subprocesses.

Defining and Accessing Variables

Variables are defined by assigning values without spaces around the = operator.

Example:

bash
NAME="Alice"
AGE=30


  • Use the variable by prefixing the variable name with a dollar sign $:
bash
echo "Name is $NAME and age is $AGE"


  • To reference variable names clearly within strings, use braces {}:
bash
echo "This is ${NAME}'s file."

Variable Naming Rules

Variable naming rules require that names begin with a letter (a–z or A–Z) or an underscore (_), and any characters that follow may include only letters, numbers, or underscores. Variable names are case-sensitive, meaning var, Var, and VAR are treated as different identifiers. To avoid errors and ensure compatibility, special characters, spaces, and names that start with numbers should not be used.


Valid examples:

text
USERNAME, var_name, COUNTER1, _temp


Invalid examples:

text
1variable, var-name, user!name

Types of Variables in Shell Scripting

Although shell variables are typeless strings, conventionally, various types exist:


1. Scalar Variables: Hold single values such as strings or integers.

2. Array Variables: Store multiple values as ordered lists.

Example:

bash
fruits=("apple" "banana" "orange")
echo ${fruits[1]} # outputs "banana"


3. Read-only Variables: Declared with readonly or declare -r keyword; cannot be modified or unset.

Example:

bash
readonly PI=3.1415


4. Environment Variables: Exported variables available to child processes using export.

Example:

bash
export PATH=/usr/bin:$PATH

Special Variables

1. $0: Name of the script.

2. $1, $2, ...: Positional parameters or script arguments.

3. $#: Number of arguments.

4. $?: Exit status of last command.

5. $$: Process ID of the current shell.

6. $!: Process ID of the last background command.

Working with Variables

Below are the essential ways to work with variables in scripts. They enable dynamic and flexible program behavior.


1. Reading user input:

bash
read USERNAME
echo "User entered: $USERNAME"


2. Arithmetic operations: Use $(( )) for calculating expressions.

Example:

bash
a=10
b=20
c=$((a + b))
echo $c # Outputs 30


3. String operations: Strings can be concatenated simply via juxtaposition:

bash
greeting="Hello"
name="Alice"
message="$greeting, $name"
echo $message
Samuel Wilson

Samuel Wilson

Product Designer
Profile

Class Sessions

1- What is Linux and Operating System Concepts 2- Linux History and Evolution 3- Linux Distributions and Their Purposes 4- Open Source Software and Licensing 5- Graphical User Interface (GUI) and Desktop Environments 6- Terminal Access and Command-Line Fundamentals 7- Getting Help and Command Documentation 8- File System Hierarchy and Directory Structure 9- Navigating Directories and Listing Contents 10- Creating, Copying, and Moving Files and Directories 11- Deleting Files and Directories 12- Symbolic and Hard Links 13- Understanding File Permissions Model 14- Modifying Permissions and Ownership 15- User and Group Management 16- Sudo and Privilege Escalation 17- Text Searching and Pattern Matching 18- Text Processing and Stream Editing 19- Compressing and Archiving Files 20- Text Editing and File Creation 21- Package Management Systems Overview 22- Installing and Updating Software with APT 23- Installing and Updating Software with YUM/DNF 24- Managing Software from Non-Repository Sources 25- Understanding Processes and Process Management 26- Viewing Running Processes 27- Process Control and Termination 28- Task Scheduling with Cron 29- Networking Concepts and IP Addressing 30- Viewing and Configuring Network Interfaces 31- Basic Network Troubleshooting 32- Shell Script Basics 33- Variables and Data Types 34- Conditional Logic in Scripts 35- Loops and Iteration 36- Functions and Code Reuse 37- Input/Output and User Interaction 38- System Authentication and Access Control 39- File System Security 40- Software Updates and Patching 41- Basic Firewall Concepts 42- System Information and Monitoring 43- Service and Daemon Management 44- System Boot Process and Runlevels 45- System Backup and Disaster Recovery 46- Comprehensive File System Management 47- System Automation Workflows 48- Multi-Concept Troubleshooting Scenarios 49- Continued Learning Pathways