How to Write Bash/PowerShell Scripts Using AI
Use an AI assistant to write shell scripts you can trust: clear flags, safe defaults, and a quick dry run before anything destructive.
In short: say Bash or PowerShell, describe inputs and the exact success message, forbid silent deletes, then run the script on a throwaway folder first.
Why shell scripts with AI
Every developer and DevOps engineer automates backups, log checks and deploys with Bash (Linux/Mac) or PowerShell (Windows, and now Linux too). AI writes the loop fast. One wrong rm can delete a day of work.
What you need
- Bash: default on Linux and Mac. Check:
bash --version. - PowerShell 7+: on Windows (and installable on Linux/Mac). Check:
pwsh --version. - An editor + assistant: VS Code + Copilot or Claude Code works for both.
- A scratch folder: never test a new script on production data first.
How it flows
Step 1 — Bash prompt
Write a Bash script backup_folder.sh that takes one argument (a folder path). If missing or not a directory, print Usage on stderr and exit 1. Copy the folder to $HOME/backups/<name>-YYYYMMDD-HHMMSS with cp -a. Use set -euo pipefail. Print the destination path. Only return the script.
Step 2 — Bash script (tested here)
backup_folder.sh#!/usr/bin/env bash
# backup_folder.sh — copy a folder into ~/backups/<name>-YYYYMMDD-HHMMSS
set -euo pipefail
SRC="${1:-}"
if [[ -z "$SRC" || ! -d "$SRC" ]]; then
echo "Usage: $0 /path/to/folder" >&2
exit 1
fi
NAME="$(basename "$SRC")"
STAMP="$(date +%Y%m%d-%H%M%S)"
DEST="${HOME}/backups/${NAME}-${STAMP}"
mkdir -p "$(dirname "$DEST")"
cp -a "$SRC" "$DEST"
echo "Backed up to $DEST"chmod +x backup_folder.sh
TMP=$(mktemp -d); echo hi > "$TMP/a.txt"
./backup_folder.sh "$TMP"
# -> Backed up to /home/…/backups/…-YYYYMMDD-HHMMSSStep 3 — same idea in PowerShell
Write Backup-Folder.ps1 with a mandatory -Path folder parameter. Validate the folder exists. Copy it recursively to $HOME/backups/<name>-yyyyMMdd-HHmmss. Print the destination. Stop on errors.
# Backup-Folder.ps1 — copy a folder into ~/backups/<name>-yyyyMMdd-HHmmss
param(
[Parameter(Mandatory = $true)]
[string]$Path
)
if (-not (Test-Path -LiteralPath $Path -PathType Container)) {
Write-Error "Folder not found: $Path"
exit 1
}
$name = Split-Path -Leaf $Path
$stamp = Get-Date -Format 'yyyyMMdd-HHmmss'
$dest = Join-Path $HOME "backups/${name}-${stamp}"
New-Item -ItemType Directory -Force -Path (Split-Path $dest) | Out-Null
Copy-Item -LiteralPath $Path -Destination $dest -Recurse
Write-Host "Backed up to $dest"pwsh -File Backup-Folder.ps1 -Path .\my-folder
# -> Backed up to …\backups\my-folder-…Settings and safe habits
- Rules file: “Bash: set -euo pipefail; PowerShell: $ErrorActionPreference = 'Stop'; never rm -rf without an explicit dry-run flag”.
- Ask the AI to add a
--dry-runflag for any script that deletes or overwrites. - Quote every variable:
"$SRC", not bare$SRC(spaces in paths). - Prefer
cp -a/Copy-Itemover hand-rolled loops for simple backups.
Watch out (common AI mistakes in shell)
- Unquoted variables — break on spaces; always quote.
- Missing
set -e— script continues after a failed command. rm -rf $VARwhen VAR is empty — can target the wrong path; check emptiness first.- curl | bash from the internet — do not auto-approve.
- PowerShell vs cmd.exe — say
pwsh/ PowerShell 7 in the prompt so you do not get old Windows cmd syntax.
Quick quiz
1. What does set -euo pipefail help with in Bash?
Exit on errors, treat unset variables as errors, and catch failures in pipes.
2. Where does our Bash script put the copy?
$HOME/backups/<folder-name>-YYYYMMDD-HHMMSS.
3. Why test on mktemp -d first?
So a bug cannot damage real project files.
4. Name one dangerous pattern to refuse in agent mode.
curl … | bash, or rm -rf on an unchecked variable.
Save tokens and money
Ask for one short script, not a whole deploy framework. Reuse the same backup prompt for new folders. Paste only the error line when a script fails.