Shell Scripting: Writing Shell Scripts

Join the AI Workshop to learn more about AI and how it can be applied to web development. Next cohort February 1st, 2026

The AI-first Web Development BOOTCAMP cohort starts February 24th, 2026. 10 weeks of intensive training and hands-on projects.


Shell scripting is a powerful way to automate tasks that you regularly execute on your computer.

Bash gives you a set of commands that put together can be used to create little programs, that by convention we call scripts.

Creating a Script

Scripts are stored in files. The important thing is that it must start with a “shebang” on the first line:

#!/bin/bash

This tells the system which interpreter to use.

Here’s a simple script:

#!/bin/bash
echo "Hello, World!"
ls

Making Scripts Executable

Save your script (e.g., as myscript) and make it executable:

chmod u+x myscript

Now run it:

./myscript

Comments

Lines starting with # are comments (except the shebang):

#!/bin/bash
# This is a comment
echo "Hello" # This is also a comment

Printing Output

Use echo to print to the screen:

echo "test"
echo test
echo testing something

Control Structures

If/Else Statements

if condition; then
  command
fi

With else:

if condition; then
  command
else
  anothercommand
fi

With elif:

if condition; then
  command
elif condition2; then
  anothercommand
else
  yetanothercommand
fi

Example:

#!/bin/bash
DOGNAME=Roger
if [ "$DOGNAME" == "" ]; then
  echo "Not a valid dog name!"
else
  echo "Your dog name is $DOGNAME"
fi

Testing Conditions

Use test or brackets [ ] to check conditions:

if test "apples" == "apples"; then
  echo "Apples are apples"
fi

if [ "$age" -lt 18 ]; then
  echo "Not old enough"
fi

Comparison operators:

  • -lt - lower than
  • -gt - greater than
  • -le - lower or equal
  • -ge - greater or equal
  • -eq - equal to
  • -ne - not equal to

While Loop

while condition
do
  command
done

For Loop

for item in list
do
  command
done

Example:

for i in 1 2 3 4 5
do
  echo $i
done

Case Statement

case $variable in
  pattern1) command1 ;;
  pattern2) command2 ;;
  *) default_command ;;
esac

Reading Input

Use read to get user input:

echo "Enter your name:"
read name
echo "Hello, $name!"

With a prompt:

read -p "Age: " age

Working with Parameters

Access script parameters with $0, $1, $2, etc.:

#!/bin/bash
echo "Script name: $0"
echo "First argument: $1"
echo "Second argument: $2"

Running ./myscript hello world outputs:

Script name: ./myscript
First argument: hello
Second argument: world

Special variables:

  • $# - number of arguments
  • $* - all arguments
  • $@ - all arguments (as separate strings)

Lessons in this unit:

0: Introduction
1: Introduction to Shells
2: Bash Basics
3: ▶︎ Writing Shell Scripts
4: Variables and Environment Variables
5: Loops and Arrays
6: Shell Script Functions
7: Creating Aliases
8: Tips and Tricks
9: The Fish Shell
10: Persist aliases and other configuration in Fish Shell
11: How to add a path to Fish Shell
12: Fish Shell, how to avoid recording commands to history
13: Fish Shell, how to remove the welcome message
14: How to replace all filenames with space with underscore using a shell script
15: How to update all npm packages in multiple projects that sit in subfolders