Ravindra BagaleCourses & study guides Track your progress
AI CODING ASSISTANTS · CHAPTER 3 · LANGUAGES

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

WHY

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

WHAT
  • 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

1 · Say Bash or PowerShell inputs + output text 2 · Ask for safe flags set -euo pipefail / Stop on error 3 · AI writes the script read every line 4 · Dry-run on a temp folder check the message 5 · Then use on real paths still no blind rm

Step 1 — Bash prompt

COPY THIS PROMPT (BASH)

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-HHMMSS

Step 3 — same idea in PowerShell

COPY THIS PROMPT (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
# 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

  1. Rules file: “Bash: set -euo pipefail; PowerShell: $ErrorActionPreference = 'Stop'; never rm -rf without an explicit dry-run flag”.
  2. Ask the AI to add a --dry-run flag for any script that deletes or overwrites.
  3. Quote every variable: "$SRC", not bare $SRC (spaces in paths).
  4. Prefer cp -a / Copy-Item over 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 $VAR when 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.
Rule: never let an agent run a generated script with delete or overwrite powers on your real home folder until you have tested it on a temp folder.

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

SMART AI USE

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.

Save these cards

INTERVIEW CARDAI Coding

How do you safely accept an AI shell script?

Require set -euo pipefail (Bash) or Stop on errors (PowerShell).
Test on a temp folder; refuse unchecked rm and curl|bash.
Ravindra Bagale · ravindrabagale.com/ai-coding
COMPARISON CARDAI Coding

Bash vs PowerShell

Bash: default on Linux/Mac servers.
PowerShell: native on Windows; also runs on Linux as pwsh.
Ravindra Bagale · ravindrabagale.com/ai-coding
REVISION CARDAI Coding

Shell with AI in 3

Name the shell + inputs.
Safe mode flags + clear Usage.
Dry-run on temp, then real paths.
Ravindra Bagale · ravindrabagale.com/ai-coding