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

Shell Script Basics

Lesson 32/49 | Study Time: 20 Min

Shell scripting is a powerful method to automate tasks and manage system operations in Linux. A shell script is essentially a text file containing a series of commands interpreted by the shell, such as Bash. By writing shell scripts, users can automate repetitive processes, simplify complex tasks, and improve productivity. 

What is a Shell Script?

A shell script is a file containing a sequence of shell commands. It allows execution of multiple commands as a single program. Shell scripts are interpreted, not compiled, meaning each command runs line-by-line. It typically started with a shebang line (#!/bin/bash) indicating the script interpreter.

Creating Your First Shell Script

The following are the basic steps to create and run a simple shell script. They introduce how scripts are written, saved, and executed in Linux.


1. Open a text editor and write the script, for example:

bash
#!/bin/bash
echo "Hello, World!"


2. Save the file with .sh extension, e.g., hello.sh.

3. Make the script executable:

bash
chmod +x hello.sh


4. Run the script:

bash
./hello.sh


The output will be:

text
Hello, World!

Basic Components of Shell Scripts

Below are the essential parts that allow a shell script to work effectively. They help manage data, commands, and user interaction.


1. Comments: Begin with #, used to document the script.

2. Commands: Regular Linux shell commands.

3. Variables: Store data for reuse.

Example:

bash
NAME="Alice"
echo "Hello, $NAME"


4. User Input: Read input with read.

Example:

bash
echo "Enter your name:"
read USERNAME
echo "Welcome, $USERNAME"

Control Structures

The following are the key control structures used in shell scripting. They decide how and when commands are executed.


1. Conditional Statements: Execute commands based on conditions.

Example:

bash
if [ $AGE -ge 18 ]; then
echo "Adult"
else
echo "Minor"
fi


2. Loops: Repeat commands.

for loop example:

bash
for i in 1 2 3
do
echo "Number $i"
done


while loop example:

bash
count=1
while [ $count -le 5 ]
do
echo "Count: $count"
((count++))
done

Basic Shell Commands Useful in Scripts

Running Shell Scripts

Below are the common techniques for running shell scripts. They allow scripts to receive arguments and behave dynamically.


1. Run scripts by specifying the path:

text
./script.sh


2. Or specify interpreter explicitly:

text
bash script.sh


3. Pass arguments to scripts and access them via $1, $2, etc.

Example:

bash
echo "Argument 1 is $1"


Run with:

text
./script.sh Hello


Output:

text
Argument 1 is Hello
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