Lesson 6.3

Shell Scripts

5 minutes

A shell script is a text file full of terminal commands that run in sequence. Instead of typing commands one at a time, you put them all in a file and run the file. That’s it — there’s no magic here.

AI coding tools create shell scripts constantly. Build scripts, deployment scripts, setup scripts — if your AI assistant needs to automate a multi-step process, it will probably write a .sh file.

What a Shell Script Looks Like

Here’s a simple example:

#!/bin/bash

echo "Starting deployment..."
cd ~/projects/my-app
npm run build
cp -r dist/ /var/www/html/
echo "Deployment complete."

Every line is a command you could type into the terminal yourself. The script just runs them all in order, so you don’t have to.

Try it now:

cat ~/projects/my-app/scripts/deploy.sh

Read through the file. Each line is a command — you should recognize most of them from earlier modules.

The Shebang Line

The very first line of a shell script is almost always:

#!/bin/bash

This is called the “shebang” line (named after the #! characters). It tells the system which program to use to run the script. In this case, it says “use bash to interpret these commands.”

You’ll also sometimes see #!/bin/sh or #!/usr/bin/env bash. They all do roughly the same thing — point to a shell interpreter.

The shebang line (#!/bin/bash) is not a comment and not a command you type. It's an instruction to the operating system: "when someone runs this file, use this program to interpret it." Without it, the system might not know how to execute the script.

Running a Shell Script

There are two ways to run a script:

1. Using bash directly

bash script.sh

This works even if the file doesn’t have execute permissions. You’re telling bash explicitly to read and run the file.

2. Running it as an executable

./script.sh

This only works if the file has execute permission (the x from the previous lesson). If you get a “permission denied” error, you know the fix:

chmod +x script.sh
./script.sh

Why AI Tools Create Scripts

AI coding assistants write shell scripts when they need to:

  • Automate multi-step tasks: installing dependencies, building code, deploying it
  • Create repeatable processes: something you’ll run more than once
  • Set up project environments: configuring tools, creating directories, setting permissions

When your AI tool creates a .sh file, it’s packaging up a series of commands so you (or it) can run them all at once.

Read Before You Run

This is the most important takeaway from this lesson. When an AI tool creates a shell script — or suggests you download and run one — you should always read it first.

A script can do anything you can do in the terminal. It can delete files, modify system settings, install software, or send data over the internet. Most scripts from AI tools are perfectly safe, but you should still understand what a script does before you execute it.

The habit is simple:

  1. Read the script: cat script.sh
  2. Understand what each line does — you now have the skills for this
  3. Then run it: bash script.sh or ./script.sh
The rule is simple: Never run a script you haven't read. This applies to scripts your AI tool creates, scripts you download from the internet, and scripts a tutorial tells you to copy-paste. If you can't understand what a command does, look it up or ask your AI tool to explain it before running it.

Practice

Try these commands in the terminal:

  1. cat ~/projects/my-app/scripts/deploy.sh – read a script’s contents
  2. ls -l ~/projects/my-app/scripts/deploy.sh – check its permissions
  3. chmod +x ~/projects/my-app/scripts/deploy.sh – make it executable
  4. ls -l ~/projects/my-app/scripts/deploy.sh – confirm the permission change
BlueBox Terminal