Script Fundamentals
Welcome to Interactive Bash Scripting! This course builds on the Linux Command Line Basics course and teaches you how to write powerful automation scripts.
Prerequisites
This course assumes you've completed the Linux Command Line Basics course or have equivalent knowledge of:
- Basic terminal navigation (cd, ls, pwd)
- File operations (touch, mkdir, cp, mv, rm)
- Viewing files (cat, less, head, tail)
- File permissions (chmod)
- Pipes and redirection
What is Bash Scripting?
Bash scripting is the art of combining shell commands into reusable programs. Instead of typing commands one by one, you write them in a file that can be executed repeatedly.
Why learn bash scripting?
- Automation: Automate repetitive tasks
- Efficiency: Run complex operations with a single command
- DevOps: Essential for system administration and CI/CD
- Portability: Works on any Unix/Linux system
Your First Look at a Script
Let's examine a simple script structure:
#!/bin/bash
# This is a comment describing the script
# Author: Your Name
# Define a variable
GREETING="Hello, World!"
# Display the greeting
echo "$GREETING"
# Exit successfully
exit 0
Script Components
Every bash script has these key components:
| Component | Purpose | Example |
|---|---|---|
| Shebang | Tells system which interpreter to use | #!/bin/bash |
| Comments | Document your code | # This does X |
| Variables | Store data | NAME="value" |
| Commands | Perform actions | echo, cp, mkdir |
| Exit code | Indicate success/failure | exit 0 |
Script vs Interactive Shell
When you type commands in the terminal, you're using an interactive shell. Scripts are non-interactive - they run without user intervention (unless designed to prompt for input).
Key differences:
# Interactive shell - you see prompts, can use history
$ ls
$ cd projects
# Script - runs all at once, no prompts
#!/bin/bash
ls
cd projects
Exercise: Explore Existing Scripts
Look at the backup script to understand script structure:
Script File Conventions
Follow these naming conventions:
- Use
.shextension:backup.sh,deploy.sh - Use descriptive names:
cleanup_logs.shnotscript1.sh - Use underscores or hyphens:
my-script.shormy_script.sh - Avoid spaces in filenames
Script File Locations
Common locations for scripts:
| Location | Purpose |
|---|---|
~/bin/ | Personal scripts |
/usr/local/bin/ | System-wide custom scripts |
./scripts/ | Project-specific scripts |
/etc/cron.d/ | Scheduled scripts |
Creating Your First Script File
Let's create a simple script:
# Step 1: Create the file
touch hello.sh
# Step 2: Add content (we'll learn better ways)
echo '#!/bin/bash' > hello.sh
echo 'echo "Hello from my first script!"' >> hello.sh
# Step 3: View it
cat hello.sh
Key Takeaways
- Bash scripts are text files containing shell commands
- Scripts automate repetitive tasks and complex operations
- Every script should start with a shebang (
#!/bin/bash) - Use comments to document your code
- Follow naming conventions: descriptive names with
.shextension - This course builds on Linux Command Line Basics knowledge

