25 25. Bash Scripts (and Using AI at the Command Line)
When you find yourself typing the same commands again and again, you can save them in a file and run the file instead. This page covers the basics of Bash scripts, and how to use AI safely when it writes commands and scripts for you.
25.1 What is a script?
A script is a text file that contains commands. When you run it, the shell executes the commands from top to bottom, exactly as if you had typed them one by one. Bash scripts usually have names ending in .sh.
Scripts let you repeat a task with one command (for example, setting up the folders for a new project, or backing up your files), share that task with others, and keep a record of exactly what you did.
25.2 Your first script
Open a new file in nano (any text editor works; see Writing to Files):
cd ~/Documents/bash-tutorial-files
nano hello.shType these lines, then save (Ctrl+O, Enter) and exit (Ctrl+X):
#!/bin/bash
# hello.sh: my first script
echo "Hello from a script"
echo "You are in $(pwd)"Run the script by handing it to Bash:
bash hello.shOutput:
Hello from a script
You are in /Users/maria/Documents/bash-tutorial-files
The second line of the script is a comment: Bash ignores everything from a # to the end of the line. Comments are notes for people who read the script, such as what it does and how to use it. (Comments work inside script files. Typed at the zsh prompt on a Mac, they don’t, by default; see Getting Bash on Your Computer.)
25.3 The shebang line
The first line, #!/bin/bash, is called the shebang (“hash” + “bang”, the nicknames of # and !). It tells the operating system which program should run the script when you run the file directly, as in the next section. You will also see #!/usr/bin/env bash, which finds bash by searching your PATH.
On a Mac this means that the script runs in Bash, even though your Terminal uses zsh. (The Mac’s built-in Bash is an old version, 3.2, which is fine for simple scripts like these.)
25.4 Running a script like a command: chmod +x and ./
chmod +x hello.sh
./hello.shchmod +x(“change mode: add execute permission”) marks the file as a program that may be run. Without it, a Mac answerspermission denied. (In Git Bash, a script that starts with#!usually runs even withoutchmod +x, but running it does no harm.)./hello.shmeans “the filehello.shin this folder.” If you type justhello.sh, the shell looks for it only in the folders on yourPATH, and your current folder isn’t one of them, so you getcommand not found. This rule protects you from accidentally running a program just because it happens to sit in the current folder.
25.5 Variables and arguments
Scripts become really useful when they take input. Here is a script that sets up a starter folder for a new project:
#!/bin/bash
# new-project.sh: create a starter folder for a new project
# Usage: bash new-project.sh PROJECT-NAME
project="$1"
mkdir -p "$project/docs" "$project/src"
echo "# $project" > "$project/README.md"
echo "Created $project with docs/, src/ and README.md"bash new-project.sh club-siteOutput:
Created club-site with docs/, src/ and README.md
$1is the first argument: the first word you typed after the script’s name (club-site).$2is the second one, and so on. (That’s why$5vanished on the quotes page.)project="$1"stores the argument in a variable with a readable name.- The double quotes around
"$project"keep a name that contains spaces in one piece. echo "# $project" > "$project/README.md"writes a heading into a new README file. This#is inside quotes, so it’s ordinary text, not a comment.
Run it again with another name, such as bash new-project.sh budget-app, and you get a second, identical setup in seconds. That’s the point of a script.
25.6 Using AI safely at the command line
AI assistants are very good at writing commands and scripts, and they are sometimes confidently wrong. You are responsible for what runs on your computer. A good routine:
- Tell the AI your system and shell: “Mac, zsh in Terminal” or “Windows, Git Bash”.
- Ask it to explain each part of a command before you run it.
- Check where you are with
pwd; most commands act on the current folder. - Preview first. Run
lswith the same wildcard before anrmormv, and test new scripts on a copy of your files. - Ask for comments in any script the AI writes, and read the script before you run it.
Never run commands like these unless you understand exactly what they will do:
| Pattern | Why it’s risky |
|---|---|
rm -rf ... |
deletes files and whole folders permanently, without asking |
sudo ... |
(Mac and Linux) runs the command with administrator rights, so it can change or break anything on the computer |
curl … | bash |
downloads a script from the internet and runs it immediately, before anyone has read it |
... > important-file |
replaces everything in the file |
Some official installers do work this way: Homebrew’s installer for the Mac, for example, downloads a script with curl and runs it with Bash. That’s acceptable when you copied the command from the official website yourself, not when it comes from a forum post or an AI’s suggestion.
A good request looks like this:
I'm on a Mac using zsh. Write a Bash script that copies every .csv file in
the current folder into a new folder named backup. Add a comment to each
line, and tell me how to test it safely first.
25.7 Preview: Claude Code runs commands for you
As of Oct 6, 2026 · Official docs: Choose a permission mode · Advanced setup · Interactive mode
Claude Code doesn’t just suggest commands: it can run them itself, through its Bash tool. (On Windows, the Bash tool uses Git Bash, so it needs Git for Windows; without it, Claude Code runs commands through a PowerShell tool instead.) Whether it asks you first depends on its permission mode. In Manual mode, it asks before running shell commands. In auto mode, which recent versions normally start in, a separate safety check reviews most actions instead of you and blocks the risky ones. Either way, you can review the commands it ran (for example in its transcript view, opened with Ctrl+O), and they are the same kind of Bash commands you learned in this part of the book. Permission modes are covered in Permissions, Modes, and Safety.
Practice Questions
Easy
Question 1 · Short answer
What is a Bash script?
Question 2 · Multiple choice
What does the first line #!/bin/bash of a script do?
- Nothing; it’s a comment
- It tells the operating system to run the script with Bash when you run the file directly
- It deletes the previous version of the script
- It makes the script executable
Question 3 · True or false
In a script, Bash runs the lines that start with # (after the first line) as commands.
Question 4 · Short answer
What does chmod +x backup.sh do?
Question 5 · Multiple choice
You run bash greet.sh Maria Lopez. Inside the script, what is the value of $2?
greet.shMariaLopezMaria Lopez
Medium
Question 6 · Write the command
Write the two commands that make report.sh executable and then run it from the current folder.
Question 7 · Short answer
hello.sh is in the current folder and is executable, but typing hello.sh gives command not found, while ./hello.sh works. Why?
Question 8 · Predict the output
greet.sh contains:
#!/bin/bash
name="$1"
echo "Hello, $name. Welcome to the course."What do these two commands print?
bash greet.sh Sam
bash greet.sh "Maria Lopez"Question 9 · Find the bug
The script total.sh contains these lines:
#!/bin/bash
total = 5
echo "Total: $total"Running it prints total.sh: line 2: total: command not found and then Total:. What’s wrong?
Question 10 · Write the script
Write a script backup-notes.sh that copies meeting-notes.txt to meeting-notes-backup.txt and then prints Backup saved. Include a shebang and a comment. Show how to run it.
Question 11 · Matching
Match each command pattern with its risk.
| Pattern | Risk | |
|---|---|---|
1. rm -rf old-site |
A. downloads a script and runs it before anyone has read it | |
2. sudo ... |
B. replaces the contents of a file | |
3. curl -fsSL https://example.com/x.sh | bash |
C. permanently deletes a folder and everything in it, without asking | |
4. echo "" > notes.txt |
D. runs a command with administrator rights |
Question 12 · Short answer
List four things you should tell or ask an AI assistant when you want it to write a command for you.
Hard
Question 13 · Scenario
Maria runs bash new-project.sh but forgets to type a project name. The script prints errors about /docs, /src, and /README.md. What happened, and how could the script protect itself?
Question 14 · Scenario
While you’re trying to fix an error, a forum post suggests:
curl -fsSL https://fix-my-setup.example.net/repair.sh | bashExplain what this would do and what you should do instead.
Question 15 · Scenario
An AI assistant says: “To fix this, run sudo rm -r /usr/local/lib/node_modules.” Sam is on Windows with Git Bash; Maria is on a Mac. What does the command do, and how should each of them respond?
Question 16 · Long answer
Claude Code can run shell commands for you, and in auto mode a safety check approves most of them without asking. Why is it still important that you can read Bash commands?
Question 17 · Ordering
Put these steps in order to create and run a new script:
- Run
./tidy.shand check the output. - Type the commands into the file, starting with
#!/bin/bash. - Run
chmod +x tidy.sh. - Open a new file with
nano tidy.sh. - Save with
Ctrl+O,Enter, and exit withCtrl+X.