AI from the CLI with Claude
Add an AI "ask" Command to Your Terminal with Claude Code
I use Ghostty as my terminal, and every so often I just want quick help with a CLI command. What was that tar syntax again? How do I find all files over 1GB? What does this one-liner from a KB article actually do?
I looked at AI terminals like Warp, but I already pay for Claude Code. So instead of adding another subscription, I built two small shell functions, ask and explain, that use Claude Code's non-interactive mode to answer command questions right in my terminal.
They work in any terminal (Ghostty, iTerm2, Terminal.app, Windows Terminal) and in both zsh and PowerShell.
Prerequisites
- Claude Code installed and signed in
- Confirm it works:
claude --version
How It Works
Claude Code has a print mode (claude -p) that takes a prompt, returns an answer, and exits without starting an interactive session. Pair it with --model haiku and you get fast answers that use less of your plan's quota.
Setup for zsh (macOS)
Open your zsh config file:
nano ~/.zshrc
Add these two functions at the bottom:
# Ask Claude for a shell command
ask() {
claude -p --model haiku "Give me only the shell command (macOS/zsh) to do this, with a one-line explanation. No extra text: $*"
}
# Ask Claude to explain a shell command
explain() {
claude -p --model haiku "Explain this shell command briefly, piece by piece: $*"
}
Save and exit (Ctrl+O, Enter, Ctrl+X), then load the changes into your current session:
source ~/.zshrc
You only do this once. zsh loads ~/.zshrc every time a new terminal opens.
Setup for PowerShell
Create your profile if it doesn't exist, then open it:
if (!(Test-Path $PROFILE)) { New-Item -Path $PROFILE -ItemType File -Force }
code $PROFILE # or: nano $PROFILE
Add these functions:
# Ask Claude for a PowerShell command
function ask {
$q = $args -join ' '
claude -p --model haiku "Give me only the PowerShell command to do this, with a one-line explanation. No extra text: $q"
}
# Ask Claude to explain a command
function explain {
$q = $args -join ' '
claude -p --model haiku "Explain this command briefly, piece by piece: $q"
}
Save, then load it into your current session:
. $PROFILE
Verify the Functions Are Loaded
# zsh
type ask
# PowerShell
Get-Command ask
Examples
Get a command:
ask find all files over 1GB in my home folder
ask list all VMs that are powered off with PowerCLI
Explain a command:
explain tar -xzvf file.tar.gz
Tips
Quote commands with special characters. When you use explain on a command containing |, *, >, or &, wrap it in quotes. Otherwise the shell interprets those characters before the function sees them:
explain "ps aux | grep ssh | awk '{print \$2}'"
Pipe errors to Claude. Send a failed command's output straight to Claude for a fix:
some-command 2>&1 | claude -p "why did this fail and how do I fix it?"
Use full Claude Code for bigger tasks. For anything multi-step, run claude in a split pane so it can execute commands and iterate. Inside a session, a line starting with ! runs as a shell command directly.
Be careful with sensitive output. Anything you send goes to a cloud model, so don't pipe output that contains passwords, keys, or customer data.
Wrap Up
Two small functions, a one-time setup, and no new subscription. If you already use Claude Code, ask and explain make it a handy command-line helper in whatever terminal you prefer.
Let me know how you customize them!