Chapter 6.1 - Introduction to Shell Scripting
Learning Objectives
By the end of this chapter, you will be able to:
- Understand what a shell script is
- Create your first Bash script
- Understand the shebang
- Make a script executable
- Use basic variables
What is a Shell Script?
A shell script is a file containing a series of shell commands that are executed sequentially. It is a powerful way to automate tasks.
Advantages:
- Automation of repetitive tasks
- Combination of complex commands
- Reusability
- Scheduling with cron
First Script
Create a Script
#!/bin/bash
# My first script
echo "Hello Linux!"
echo "Date: $(date)"
echo "User: $(whoami)"
Steps:
- Create the file:
nano my_script.sh - Add the content above
- Make it executable:
chmod +x my_script.sh - Execute:
./my_script.sh
Shebang
The first line #!/bin/bash is the shebang:
- Indicates which interpreter to use
- Necessary for direct execution
- Alternatives:
#!/bin/sh,#!/usr/bin/env bash
Variables
Declaration
#!/bin/bash
# Simple variables
NAME="Linux"
VERSION=6.1
ACTIVE=true
# Usage
echo "Name: $NAME"
echo "Version: $VERSION"
echo "Active: $ACTIVE"
Rules:
- No spaces around the
= - Quotes for strings with spaces
$to use the variable
Environment Variables
#!/bin/bash
# System variables
echo "Home: $HOME"
echo "User: $USER"
echo "Path: $PATH"
echo "PWD: $PWD"
Special Variables
#!/bin/bash
echo "Script name: $0"
echo "First argument: $1"
echo "Second argument: $2"
echo "Number of arguments: $#"
echo "All arguments: $@"
echo "Return code: $?"
Reading Input
read
#!/bin/bash
# Read input
echo "Enter your name:"
read NAME
echo "Hello $NAME!"
# With integrated prompt
read -p "Enter your age: " AGE
echo "You are $AGE years old"
# Silent read (password)
read -s -p "Password: " PASS
echo
Comments
#!/bin/bash
# Single line comment
# Multi-line
# comment
# End of line comment
COMMAND # Explanation
Complete Example
#!/bin/bash
# Example script
# Author: Your name
# Date: $(date +%Y-%m-%d)
# Variables
SCRIPT_NAME="my_script.sh"
VERSION="1.0"
# Main function
main() {
echo "=== $SCRIPT_NAME v$VERSION ==="
echo "Date: $(date)"
echo "User: $USER"
echo "Directory: $PWD"
echo "Arguments: $#"
if [ $# -gt 0 ]; then
echo "First argument: $1"
fi
}
# Execution
main "$@"
Execution
Methods
# 1. Direct execution (requires shebang and chmod +x)
./my_script.sh
# 2. With explicit bash
bash my_script.sh
# 3. With sh
sh my_script.sh
# 4. Source (execute in current shell)
source my_script.sh
. my_script.sh # Shortcut
Best Practices
Structure
#!/bin/bash
# 1. Shebang
# 2. Header comments
# 3. Global variables
# 4. Functions
# 5. Main code
Useful Options
#!/bin/bash
set -e # Stop on error
set -u # Error if undefined variable
set -o pipefail # Error in pipeline
# Or combine
set -euo pipefail
Summary
In this chapter, you learned:
Shell script: File of executable commands
Shebang: #!/bin/bash to indicate the interpreter
Variables: Declaration and usage with $
Reading: read for user input
Execution: ./script.sh, bash script.sh, source script.sh
Best practices: Structure, set -e, comments
Next Steps
Chapter 6.2: Variables and Parameters
Chapter 6.3: Control Structures
Chapter created on: December 2024