Shell Script · Lesson

Shell Scripting · Lesson 1

Introduction to Shell Scripting

Shell scripting is one of the most powerful tools for Unix/Linux admins and DevOps engineers. In this lesson, we’ll see what a shell is, what a shell script actually does, why you should use it, and how to run your very first script safely.

Add your shell scripting intro video URL in ShellLessons.js.

What is a shell? What is a shell script?

Shell

A program that takes your commands and asks the OS to execute them. Examples: bash, sh, ksh, zsh.

Shell script

A plain text file containing a list of shell commands, executed in sequence by the shell.

Automation

Instead of typing commands again and again, you automate them with logic, loops, conditions and functions.

In simple terms: when you type commands at the terminal, you are already scripting manually. A shell script is just you saving those commands into a file, adding logic, and running them again and again in a repeatable way.

Why shell scripting is important in real life

  • Daily admin tasks: backups, log cleanup, user management.
  • Monitoring and alerts: check disk usage, services, processes.
  • Automation glue: connect different tools/commands into one workflow.
  • Repeatable tasks: same script can run on many servers with small changes.

How does the shell execute commands?

When you type a command like ls /var/log, the shell:

  1. Reads your input line.
  2. Splits it into command (ls) and arguments.
  3. Searches for the command in your PATH.
  4. Starts the program and passes arguments to it.
  5. Waits for it to finish and receives an exit status (0 = success, non-zero = some error).
Example: shell resolving and running a command
shell-intro
# Print current shell
echo $SHELL
 
# Show where 'ls' comes from
which ls
 
# A simple command
ls /var/log
 
# Check last command exit status
echo $? # 0 means success

In scripts, you will use the exit status $? a lot to decide if previous command succeeded or failed.

Writing your first shell script (safely)

A shell script is just a text file. We usually start with a shebang line that tells which shell should execute the file.

first_script.sh
shell-intro
#!/bin/bash
# My first shell script
 
echo "Hello from shell script!"
echo "Today is: $(date)"
echo "You are logged in as: $USER"
echo "Your current directory is: $(pwd)"

Explanation:

  • #!/bin/bash – shebang; tells the OS to use /bin/bash to run this file.
  • Lines starting with # (except shebang) are comments and ignored by the shell.
  • $(date) and $(pwd) are command substitutions: run the command and insert its output there.

Making the script executable and running it

Running the first script
shell-intro
# 1. Create the file
nano first_script.sh # or vi / vim / any editor
# (paste script content and save)
 
# 2. Make it executable
chmod +x first_script.sh
 
# 3. Run it explicitly with ./ (from current directory)
./first_script.sh
 
# Or call with bash without +x
bash first_script.sh

Real-life practice: always start with harmless scripts like echo, date, pwd, whoami. Never start learning by writing scripts that delete or move files.

Where to keep scripts & basic permissions

Good habits from day one

  • Create a dedicated directory for your personal scripts, like ~/scripts or ~/bin.
  • Always use meaningful file names: backup_home.sh, user_cleanup.sh, etc.
  • Use executable permissions carefully (chmod +x) – not every file needs 777.
Organising your scripts
shell-intro
# Create a directory for your scripts
mkdir -p ~/scripts
 
# Move your script there
mv first_script.sh ~/scripts/
 
# Optional: add ~/scripts to PATH in ~/.bash_profile or ~/.profile
echo 'export PATH="$HOME/scripts:$PATH"' >> ~/.bash_profile
 
# Reload profile (or logout/login)
source ~/.bash_profile
 
# Now you can run script from anywhere
first_script.sh

Real-life example: “In admin environments, we keep custom scripts under a common path like /opt/scripts or /usr/local/sbin with version control. For personal practice, ~/scripts or ~/bin is perfect.”

Practical use cases even for beginners

Let’s look at a small but real script you might write as a junior admin: checking disk usage and warning when it crosses a limit.

Simple disk usage check script
shell-intro
#!/bin/bash
# check_disk.sh - warn if any filesystem crosses 80% usage
 
THRESHOLD=80
 
echo "Checking disk usage... (threshold: ${THRESHOLD}%)"
echo
 
# Use df -hP to get a consistent format (POSIX)
df -hP | awk 'NR>1 {print $5 " " $6}' | while read use mount; do
# Strip the % sign
PERCENT=${use%%%}
 
if [ "$PERCENT" -ge "$THRESHOLD" ]; then
echo "WARNING: Filesystem $mount is at ${PERCENT}% usage"
else
echo "OK : Filesystem $mount is at ${PERCENT}% usage"
fi
done
  • This is already useful in real environments, and you wrote it with only basic shell features.
  • Later in the course, we’ll upgrade this script: add logging, email alerts, and schedule it via cron.

What you should be comfortable with after this lesson

  • Explain what a shell and a shell script are.
  • Create, save and run a simple script using chmod +x and ./script.sh.
  • Understand shebang (#!/bin/bash) and comments.
  • Know where to keep your scripts and how to add to PATH.

In the next lessons, we’ll dive into variables, quoting, user input, conditions and loops so you can build powerful real-world automation.