Shell Scripts
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.
#!/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:
- Read the script:
cat script.sh - Understand what each line does — you now have the skills for this
- Then run it:
bash script.shor./script.sh
Practice
Try these commands in the terminal:
cat ~/projects/my-app/scripts/deploy.sh– read a script’s contentsls -l ~/projects/my-app/scripts/deploy.sh– check its permissionschmod +x ~/projects/my-app/scripts/deploy.sh– make it executablels -l ~/projects/my-app/scripts/deploy.sh– confirm the permission change