Practical examples of shell script examples for beginners

If you’re trying to actually *use* the command line instead of just staring at it, you’re in the right place. This guide walks through practical, hands-on examples of shell script examples for beginners that you can copy, tweak, and make your own. Instead of abstract theory, we’ll focus on tiny, real scripts that solve everyday problems. You’ll see each example of a shell script explained line by line, with plain-English commentary. By the end, you’ll know how to automate boring tasks, work with files, add simple logic, and even build a tiny backup tool. These are the kinds of examples include in real developer workflows, not just classroom exercises. We’ll stick to Bash, since it’s the default shell on most Linux systems and is still widely used in 2024–2025 on servers, Docker containers, and developer machines. Open a terminal, create a test folder, and get ready to run some of the best examples of beginner-friendly shell scripts you’ll actually use.
Written by
Taylor
Published

Start with simple, real examples (not theory)

Let’s jump straight into practical examples of shell script examples for beginners. You can run these on macOS or most Linux distributions using Bash. On Windows, you can use WSL (Windows Subsystem for Linux) or Git Bash.

Before anything else, create a sandbox folder so you don’t mess up real files:

mkdir -p ~/shell-playground
cd ~/shell-playground

Every script you create below should be saved in this folder, then made executable with:

chmod +x script-name.sh

Then run it with:

./script-name.sh

We’ll build from very small scripts to slightly more capable ones, so you feel the progression.


Example of a “Hello and date” script

This is the classic first example of a shell script, but let’s make it just a little more useful.

Create a file named hello_date.sh:

#!/usr/bin/env bash

echo "Hello, $(whoami)!"
echo "Today is: $(date)"

Make it executable and run it:

chmod +x hello_date.sh
./hello_date.sh

What’s happening:

  • #!/usr/bin/env bash tells the system to run this with Bash.
  • whoami prints your username.
  • date prints the current date and time.

This is one of the simplest examples of shell script examples for beginners: it shows how to run commands, use command substitution $(...), and print output to the terminal.


Example of a script that greets a user by name

Now let’s accept input. This gives you a taste of interactive scripts.

Create greet_user.sh:

#!/usr/bin/env bash

read -p "What is your name? " name

if [[ -z "$name" ]]; then
  echo "You didn’t type a name. I’ll just call you 'friend'."
  name="friend"
fi

echo "Nice to meet you, $name!"

Run it:

./greet_user.sh

Key ideas in this example of a script:

  • read -p prompts the user for input.
  • The if [[ -z "$name" ]] test checks if the variable is empty.
  • You see your first if/then block, which is used constantly in real examples of shell scripts in production.

Best examples of file and folder automation scripts

One of the best examples of why people learn shell scripting is simple file housekeeping. Let’s look at a few real examples that you might actually use.

Script: Organize downloads by file type

Say you have a messy Downloads folder. Create organize_downloads.sh:

#!/usr/bin/env bash

DOWNLOADS="$HOME/Downloads"
cd "$DOWNLOADS" || exit 1

mkdir -p Images Documents Archives Other

for file in *; do
#  # Skip directories
  [[ -d "$file" ]] && continue

  case "$file" in

    *.jpg|*.jpeg|*.png|*.gif)
      mv "$file" Images/ ;;

    *.pdf|*.docx|*.txt)
      mv "$file" Documents/ ;;

    *.zip|*.tar|*.tar.gz|*.tgz)
      mv "$file" Archives/ ;;

    *)
      mv "$file" Other/ ;;
  esac
done

echo "Downloads organized by file type."

This is one of the more realistic examples of shell script examples for beginners because it introduces:

  • Variables (DOWNLOADS)
  • The for loop
  • The case statement for pattern matching
  • Basic safety (cd "$DOWNLOADS" || exit 1)

You can tweak the file extensions to match your own workflow.


Script: Bulk rename files with a prefix

Another example of shell script automation: renaming a bunch of files at once.

Create add_prefix.sh:

#!/usr/bin/env bash

prefix="$1"

if [[ -z "$prefix" ]]; then
  echo "Usage: $0 PREFIX"
  exit 1
fi

for file in *; do
  [[ -d "$file" ]] && continue
  mv "\(file" "}\(prefix}_$file"
  echo "Renamed \(file to }\(prefix}_$file"
done

Usage:

./add_prefix.sh project

This example of a script shows:

  • Using command-line arguments ($1)
  • Basic validation and usage messages
  • Looping over files and renaming them

These are the kinds of examples include in day-to-day developer tooling scripts.


Examples of shell script examples for beginners using conditions and loops

Once you’re comfortable running basic scripts, conditions and loops are where things start to feel powerful.

Script: Check disk usage and warn if it’s high

This script checks your root filesystem and warns you if it’s getting full.

Create disk_check.sh:

#!/usr/bin/env bash

THRESHOLD=80

usage=\((df -h / | awk 'NR==2 {gsub("%", "", \)5); print $5}')

if (( usage > THRESHOLD )); then
  echo "Warning: disk usage is at \({usage}% (threshold: }\(THRESHOLD}%)."
else
  echo "Disk usage is fine: \({usage}% (threshold: }\(THRESHOLD}%)."
fi

Run it:

./disk_check.sh

This is one of the best examples of mixing shell commands with logic:

  • df -h / shows disk usage.
  • awk extracts the percentage.
  • (( usage > THRESHOLD )) uses arithmetic comparison.

On servers, real examples of shell scripts like this get scheduled with cron to send alerts.


Script: Simple backup of a folder with timestamp

Backups are a classic example of shell script examples for beginners that become permanent fixtures in your toolkit.

Create backup_folder.sh:

#!/usr/bin/env bash

SOURCE_DIR="$1"
BACKUP_ROOT="$HOME/backups"

if [[ -z "$SOURCE_DIR" ]]; then
  echo "Usage: $0 /path/to/folder"
  exit 1
fi

if [[ ! -d "$SOURCE_DIR" ]]; then
  echo "Error: $SOURCE_DIR is not a directory."
  exit 1
fi

mkdir -p "$BACKUP_ROOT"

TIMESTAMP=$(date +"%Y%m%d-%H%M%S")
BASENAME=\((basename "\)SOURCE_DIR")
BACKUP_FILE="\(BACKUP_ROOT/}\(BASENAME}-${TIMESTAMP}.tar.gz"

tar -czf "\(BACKUP_FILE" -C "\)(dirname "\(SOURCE_DIR")" "\)BASENAME"

echo "Backup created: $BACKUP_FILE"

Usage:

./backup_folder.sh ~/Documents

Concepts this example of a backup script teaches:

  • Validating arguments and directories
  • Creating timestamped filenames
  • Using tar for compressed archives

If you later add this to cron, you’ve got a simple backup system that’s very common in real examples of shell script usage in small teams and home labs.


Examples include working with text and logs

Shell scripts shine when you’re wrangling text. Let’s look at another example of a script that inspects logs.

Script: Count how many times a word appears in a file

Create count_word.sh:

#!/usr/bin/env bash

file="$1"
word="$2"

if [[ -z "\(file" || -z "\)word" ]]; then
  echo "Usage: $0 FILE WORD"
  exit 1
fi

if [[ ! -f "$file" ]]; then
  echo "Error: $file does not exist or is not a regular file."
  exit 1
fi

count=\((grep -o -i "\)word" "$file" | wc -l)

echo "The word '\(word' appears \)count times in $file."

Usage:

./count_word.sh /var/log/syslog error

This is one of those simple but powerful examples of shell script examples for beginners that you can adapt to many tasks:

  • grep -o -i finds case-insensitive matches, one per line.
  • wc -l counts lines.
  • You combine small Unix tools to do something surprisingly handy.

A slightly more advanced example: A tiny menu-driven tool

To round things out, here’s a small, menu-based script that bundles a few actions together. This is closer to what you might see in real examples of internal tools on a team.

Create system_menu.sh:

#!/usr/bin/env bash

while true; do
  echo ""  # blank line
  echo "System Helper Menu"
  echo "-------------------"
  echo "1) Show disk usage"
  echo "2) Show top 5 memory processes"
  echo "3) Show logged-in users"
  echo "4) Quit"
  read -p "Choose an option [1-4]: " choice

  case "$choice" in
    1)
      df -h
      ;;
    2)
      ps aux --sort=-%mem | head -n 6
      ;;
    3)
      who
      ;;
    4)
      echo "Goodbye!"
      exit 0
      ;;

    *)
      echo "Invalid option. Please choose 1-4."
      ;;
  esac
done

Run it and play with the menu. This example of a shell script brings together:

  • An infinite while true loop
  • A case statement for user choices
  • Mixing multiple system commands in one tool

Once you understand this pattern, you can build your own mini-toolkit scripts that wrap common commands.


How these examples fit into 2024–2025 workflows

You might wonder if shell scripting still matters in a world of Docker, Kubernetes, and fancy CI/CD platforms. The short answer: yes, very much.

In 2024–2025, real examples of shell scripts show up everywhere:

  • In Docker images, where entrypoint scripts configure applications at startup.
  • In GitHub Actions, GitLab CI, and other CI pipelines, where short Bash snippets glue tools together.
  • In DevOps and SRE work, where quick one-off scripts manage logs, users, and deployments.

Even if you later move most of your automation into Python or another language, learning from examples of shell script examples for beginners gives you a fast way to:

  • Understand existing infrastructure scripts written years ago.
  • Prototype ideas quickly right in the terminal.
  • Automate repetitive tasks without setting up a full project.

For deeper learning, many universities still teach shell scripting as part of operating systems or systems programming courses. For example, you can find shell and Unix tutorials through resources like the MIT OpenCourseWare platform and broader computer science study guides at Harvard University, both of which regularly use command-line tools in their coursework.


Tips for writing your own beginner-friendly shell scripts

As you adapt these examples of shell script examples for beginners, a few habits will save you time and frustration:

  • Start small. Write a tiny script that does one thing, run it, then grow it step by step.
  • Add echo statements while debugging so you can see variable values.
  • Use set -e near the top of more serious scripts to exit on errors, once you’re comfortable.
  • Keep scripts in a personal ~/bin folder and add it to your PATH so you can run them from anywhere.

And always test on dummy data first. It’s easy to move or delete the wrong files if you’re not careful.


FAQ: Common beginner questions about shell script examples

Q: What are some simple examples of shell script examples for beginners I should start with?
Start with tiny scripts: a greeting script, a script that prints today’s date, one that lists files in a directory, and a basic backup script. The examples on this page—like hello_date.sh, greet_user.sh, and backup_folder.sh—are good first steps.

Q: Can you give an example of a script that runs every day automatically?
Yes. Take the backup_folder.sh example and add it to your crontab with a line like:

0 2 * * * /home/youruser/shell-playground/backup_folder.sh /home/youruser/Documents

That runs the script every day at 2:00 AM. Many real examples of shell scripts in production are just simple tools wired into cron like this.

Q: Is Bash still worth learning in 2025, or should I skip straight to Python?
Both are useful. Bash is great for gluing existing commands together and running quick tasks. Python shines for larger programs and complex logic. Most engineers know enough Bash to read and write short scripts, then switch to Python or another language when things get big.

Q: Where can I learn more beyond these beginner examples?
You can explore free command-line and shell resources from universities and educational organizations. For instance, MIT’s OpenCourseWare at https://ocw.mit.edu includes Unix and programming materials, and Harvard’s CS50 course at https://cs50.harvard.edu uses the command line extensively. The GNU Bash manual at https://www.gnu.org is the formal reference for Bash features.

Q: How do I avoid breaking my system with a script?
Work in a test directory, avoid running scripts as root unless absolutely required, and always print what you plan to do before you actually do it. For example, first use echo mv "$file" target/ to see the command, then remove echo once you’re confident.


If you keep experimenting with these examples of shell script examples for beginners—tweaking, breaking, and fixing them—you’ll be surprised how quickly the command line starts to feel like a power tool instead of something to be afraid of.

Explore More Shell Scripting Snippets

Discover more examples and insights in this category.

View All Shell Scripting Snippets