3.3 Turning Commands into a Script (Weight: 4)
📘Linux Essentials (LPI 010-160)
1. What is a Shell Script?
A shell script is a text file that contains a sequence of commands which are executed by the Linux shell.
The shell reads the file line by line and executes each command exactly as if the user typed it in the terminal.
Key Points
- A shell script contains Linux commands written in order.
- The script is interpreted by a shell program.
- Scripts automate repetitive administrative tasks.
Example Script
#!/bin/bash
date
whoami
uptime
When this script runs, it will:
- Display the current date and time
- Display the current user
- Display system uptime
All commands run automatically.
2. The Shell
The shell is the program that interprets commands.
It provides the interface between the user and the Linux operating system.
Common Linux shells include:
| Shell | Description |
|---|---|
| sh | Original Unix shell |
| bash | Bourne Again Shell (most common) |
| zsh | Advanced interactive shell |
| dash | Lightweight POSIX shell |
In Linux Essentials, Bash is the most commonly used shell.
3. Script File Basics
A shell script is simply a plain text file.
Typical steps to create a script:
Step 1: Create the file
nano script.sh
Step 2: Write commands
#!/bin/bash
echo "System Information"
uname -a
Step 3: Save the file
Step 4: Make it executable
chmod +x script.sh
Step 5: Run the script
./script.sh
4. The Shebang (#!)
The shebang defines which interpreter should run the script.
It is always placed on the first line of the script.
Example:
#!/bin/bash
Meaning:
#!→ special characters indicating interpreter/bin/bash→ the shell used to run the script
Other examples:
#!/bin/sh
#!/usr/bin/env bash
The system reads this line to determine which program should execute the script.
5. Making Scripts Executable
By default, a script file does not have execute permission.
You must enable execution using:
chmod +x script.sh
This adds execute permission to the file.
To check permissions:
ls -l script.sh
Example output:
-rwxr-xr-x 1 user user 120 script.sh
x indicates executable permission.
6. Running a Script
There are several ways to run a shell script.
Method 1 — Execute directly
./script.sh
./ indicates the current directory.
Method 2 — Run using bash
bash script.sh
This tells Bash to interpret the script.
Method 3 — Run using sh
sh script.sh
This uses the sh shell interpreter.
7. Comments in Scripts
Comments are lines ignored by the shell. They are used to explain the script.
Comments begin with the # symbol.
Example:
# Display system information
uname -a
Example script:
#!/bin/bash# Print system details
echo "System Info"
uname -r
Comments improve readability and documentation.
8. Variables in Shell Scripts
A variable stores a value that can be reused later in the script.
Creating Variables
name="server01"
Important rule:
There must be no spaces around =.
Correct:
user="admin"
Incorrect:
user = "admin"
Using Variables
To access a variable, use $.
Example:
echo $name
Example script:
#!/bin/bashserver="web01"
echo "Server name is $server"
Output:
Server name is web01
9. Command Substitution
Command substitution allows a command’s output to be stored in a variable.
Two methods exist:
Modern method
$(command)
Example:
current_user=$(whoami)
echo $current_user
Older method
`command`
Example:
date=`date`
However, the modern syntax is preferred.
10. Environment Variables
Environment variables are predefined variables provided by the shell.
Common variables:
| Variable | Meaning |
|---|---|
$HOME | User home directory |
$USER | Current username |
$PATH | Command search path |
$PWD | Current directory |
Example:
echo $HOME
Example script:
#!/bin/bashecho "User: $USER"
echo "Home directory: $HOME"
11. Quoting in Shell Scripts
Quoting controls how the shell interprets text.
Double Quotes " "
Variables are expanded.
Example:
name="server01"
echo "Server is $name"
Output:
Server is server01
Single Quotes ' '
Variables are not expanded.
Example:
echo 'Server is $name'
Output:
Server is $name
12. Escape Characters
The backslash \ is used as an escape character.
It prevents special interpretation.
Example:
echo "This is a \$variable"
Output:
This is a $variable
13. Exit Status
Every command returns an exit status.
0→ success- non-zero → error
You can access the last exit status with:
$?
Example:
ls /etc
echo $?
Example script:
#!/bin/bashls /etc
echo "Exit status: $?"
System administrators use exit status to check whether commands succeed.
14. Simple Conditional Execution
Basic shell scripting allows commands to run based on conditions.
Basic syntax:
if command
then
commands
fi
Example:
#!/bin/bashif ping -c 1 server01
then
echo "Host reachable"
fi
This executes commands only if the condition succeeds.
15. Script Arguments
Scripts can receive parameters from the command line.
Example:
./script.sh file1
Special variables:
| Variable | Meaning |
|---|---|
$0 | script name |
$1 | first argument |
$2 | second argument |
$# | number of arguments |
$@ | all arguments |
Example script:
#!/bin/bashecho "Script name: $0"
echo "First argument: $1"
Run:
./script.sh data.txt
Output:
Script name: ./script.sh
First argument: data.txt
16. Basic Script Structure
A typical shell script contains:
#!/bin/bash# comments describing the script# variables
server="web01"# commands
echo "Checking server $server"uptime
df -h
Scripts often follow this order:
- Shebang
- Comments
- Variables
- Commands
17. Common IT Tasks Automated with Scripts
Shell scripts are frequently used to automate system administration tasks.
Examples include:
System information reports
#!/bin/bash
hostname
uptime
free -m
Checking disk usage
#!/bin/bash
df -h
Monitoring running processes
#!/bin/bash
ps aux
Running backup commands
#!/bin/bash
tar -czf backup.tar.gz /home
These tasks are commonly automated in Linux environments.
18. Best Practices for Basic Scripts
For simple scripts:
- Always include a shebang
- Use clear variable names
- Add comments
- Test scripts before production use
- Ensure scripts have correct permissions
19. Important Commands Related to Scripts
| Command | Purpose |
|---|---|
chmod | change file permissions |
bash | run script with Bash |
sh | run script with sh |
echo | print output |
exit | exit script |
source | run script in current shell |
Example:
source script.sh
20. Key Exam Points to Remember
For the Linux Essentials exam, you should understand:
- What a shell script is
- Purpose of the shebang (
#!) - How to create and run scripts
- How to make scripts executable
- Variables and how they work
- Environment variables
- Command substitution
- Script arguments (
$1,$2,$@) - Quoting and escaping
- Exit status (
$?) - Basic if conditional syntax
- Use of comments
These are the core scripting concepts required for the LPI Linux Essentials 010-160 exam.
