# Write a Bash Script with an AI Agent: Prompt, Lint, Run

> Canonical: https://skillalmanac.com/learn/write-a-bash-script-with-an-ai-agent | Updated: 2026-11-22 | Index status: not offered to search engines

Steps to have an AI coding agent write a bash script, lint it with ShellCheck, and review permissions and a dry run before it touches real files.

## Facts

- 2026-11-22: ShellCheck's documentation says it makes canonical use of exit codes, so a shellcheck command can be added as a step in a build (https://github.com/koalaman/shellcheck)
- 2026-11-22: ShellCheck uses the shebang line to choose the shell it checks against, and a #!/bin/sh shebang triggers portability warnings similar to checkbashisms (https://github.com/koalaman/shellcheck)
- 2026-11-22: By default Claude Code asks for approval before a Bash command, except for a built-in set of read-only commands (https://code.claude.com/docs/en/permissions)
- 2026-11-22: The 'Yes, and don't ask again' choice for Bash commands is permanent per repository and command (https://code.claude.com/docs/en/permissions)

You can have an AI agent write a bash script by stating the inputs, outputs and limits in one prompt, then checking the result with a linter before anything runs. The steps below cover the prompt, a ShellCheck pass, a dry run on copied files, and the permission prompts that appear along the way.

## Steps

1. Open a terminal in the folder where the script will live, and start your coding agent there. It then reads that project's files and works in the same place you will run the script.

2. Write the prompt as a contract. Name the file path, the input (for example, a directory passed as the first argument), the output, and the actions the script may take. A usable prompt looks like this: "Write scripts/compress-old-logs.sh, a bash script that gzips files in the directory given as $1 that are older than 7 days. Print each file before touching it. Do not delete anything. Do not run the script yet."

3. Ask the agent to quote every variable expansion and to print a usage message when the argument is missing. These two requests catch most of the errors that a first draft from an agent contains.

4. Run ShellCheck on the file:

   `shellcheck scripts/compress-old-logs.sh`

   ShellCheck's documentation says it uses exit codes so that you can add it to a build process. Treat any finding as an unfinished review. Give the full output back to the agent and ask it to fix each reported line. Read each change, and do not accept a fix you cannot explain.

5. Make the script executable with `chmod +x scripts/compress-old-logs.sh`. This adds the execute permission, so the file can run as `./scripts/compress-old-logs.sh`.

6. Dry run on a copy. Create a throwaway folder with a few sample files, run the script against it, and compare the folder listing before and after. Only point the script at real data after the copy behaves as the prompt described.

7. Watch the permission prompts. Claude Code asks for approval before Bash commands unless they fall in a built-in read-only set. When you pick "don't ask again," that approval is stored per repository and per command, so approve the exact command line you reviewed and nothing broader.

## Check that it worked

- `shellcheck` on the file prints nothing and exits with status 0.
- The copy-folder run prints one line per file it considered, and the files it compressed are the ones older than the cutoff.
- `git status` in the project shows only the script file as new, with no stray output files.

## Pitfalls

- **Windows line endings.** A script saved with CRLF endings can fail at runtime with an error that mentions a carriage return. Convert the file to LF endings in your editor, then rerun ShellCheck.
- **Curly quotes from chat output.** Quotes copied from a chat window are often typographic rather than straight ASCII, so a quoted argument may not behave as you expect. Retype each quote as a straight ASCII character.
- **Unquoted variables.** A file named `old log.txt` splits into two arguments when the variable is unquoted. Search the script for every `$` and confirm each one sits inside double quotes unless you have a reason it does not.
- **A clean lint is not a safe script.** ShellCheck reports problems in the text of the script, not whether its logic matches your intent. A script that passes it can still delete the wrong files. Read every line that moves, deletes or overwrites data before running it on real folders.
- **A wrong shebang.** The first line sets the shell dialect that ShellCheck checks against, so a wrong shebang applies another shell's rules to your script. If the script runs under bash, start it with `#!/usr/bin/env bash`.

## When to use a skill instead

A one-time script with no file changes can go through this prompt-lint-run sequence without extra setup. A script that touches real folders, or that you will rewrite later, benefits from a reusable skill because the same safety steps run each time. [coding-agent-guardrails](/skills/skill-almanac/coding-agent-guardrails) adds a pre-action check that covers destructive commands and edits to files the agent should leave alone, which suits a script that handles a folder of live data. [systematic-debugging](/skills/skill-almanac/systematic-debugging) applies when the generated script fails: it has the agent reproduce the failure and test one change at a time instead of rewriting the script from memory. If the script is ten lines and runs once against a scratch folder, neither skill is needed.

## FAQ

### Why does Claude Code ask before each command the script needs?

By default Claude Code asks for approval before a Bash command, except for a built-in set of read-only commands. Choosing "don't ask again" saves that approval per repository and per command, so approve only the exact command line you reviewed.

### How do I make a script file runnable from the terminal?

Create a plain text file with a `.sh` extension, put a shebang such as `#!/usr/bin/env bash` on the first line, and run `chmod +x` on the file. Then run it as `./filename.sh` from the folder that holds it.

### Does a passing ShellCheck run mean the script is safe?

No. ShellCheck reports problems in the script's text, such as quoting and shell dialect, and it cannot judge whether the script deletes the files you meant. Dry-run the script on a copy before using it on real data.

## Related skills

- [coding-agent-guardrails](https://skillalmanac.com/skills/skill-almanac/coding-agent-guardrails.md): Platform Score 60/100
- [systematic-debugging](https://skillalmanac.com/skills/skill-almanac/systematic-debugging.md): Platform Score 57/100 (provisional)

## Related guides

- [Agentic Coding Tools: What They Are and How to Choose One](https://skillalmanac.com/learn/choosing-an-agentic-coding-tool.md)
- [Claude Code first session: a plan, edit, review loop](https://skillalmanac.com/learn/claude-code-first-session-workflow.md)
- [Codex vs Claude Code: Config Files and Sandbox](https://skillalmanac.com/learn/codex-vs-claude-code.md)
- [What Is Claude Code and What It Can Touch on Your Machine](https://skillalmanac.com/learn/what-is-claude-code.md)
- [How to Write a PowerShell Script with Safe Dry Runs](https://skillalmanac.com/learn/write-a-powershell-script-with-an-ai-agent.md)
- [AI Coding Assistant vs Coding Agent: What Each Can Change](https://skillalmanac.com/learn/ai-coding-assistant-vs-coding-agent.md)

## Sources

- https://github.com/koalaman/shellcheck
- https://code.claude.com/docs/en/permissions

Platform Scores report our assessment under a published method. They are not a safety guarantee.
