claude -p: "Input must be provided either through stdin" fix
claude -p Input must be provided either through stdin or as a prompt argument means the prompt was empty. Check variables and pipes before calling claude.
Error: Input must be provided either through stdin or as a prompt argument when using --printclaude -p (--print) is non-interactive and needs a prompt; this error means there was no prompt argument and nothing on standard input. Pass the prompt as an argument (claude -p "question") or pipe it in (echo "question" | claude -p). In a script, the cause is almost always an empty variable or file that was supposed to hold the prompt.
[1] Tell it apart from similar messages.
| Message | When |
|---|---|
Error: Input must be provided either through stdin or as a prompt argument when using --print | No prompt. Also when you ran plain claude and stdout is not a terminal |
Error: Input contained only whitespace. Provide a prompt with text through stdin or as a prompt argument when using --print | The prompt is only spaces, tabs or newlines (rejected before sending since v2.1.229) |
Claude Code can't read the keyboard here: stdin is not a terminal … | On Windows, run without -p while stdin is piped or redirected |
[2] If you typed plain claude and still got a --print error, you ran it where output does not go to a terminal. The interactive UI needs stdout to be a terminal. With output redirected, as in claude > out.txt, or inside PowerShell ISE or an IDE output pane, Claude Code runs in the same non-interactive mode as claude -p and asks for a prompt. For interactive use, run it in a real terminal: Windows Terminal, the PowerShell console, or your IDE's integrated terminal.
[3] For a one-shot question, pass the prompt.
$ claude -p "Summarize the diff"$ echo "Review this file" | claude -pThis also works with --continue and --resume <session-id> (cat notes.md | claude -p --continue).
[4] In scripts and CI, check that the prompt is not empty before calling claude. Passing an empty variable as is becomes claude -p "" and triggers this error. The ask.sh below reads standard input when no argument is given, rejects whitespace-only prompts too, and exits with code 2.
#!/usr/bin/env bash# ask.sh — do not call claude when the prompt is emptyprompt="${1-}"if [ -z "$prompt" ] && [ ! -t 0 ]; then prompt="$(cat)"fiif [ -z "${prompt//[[:space:]]/}" ]; then echo "ask.sh: prompt is empty or whitespace only" >&2 exit 2fiprintf '%s' "$prompt" | claude -pResults for five cases. To avoid calling a model, claude was replaced by a stub that prints only the arguments and the number of stdin bytes it received.
$ ./ask.sh ""ask.sh: prompt is empty or whitespace only$ printf ' \n' | ./ask.shask.sh: prompt is empty or whitespace only$ PROMPT=$(cat missing.txt 2>/dev/null); ./ask.sh "$PROMPT"ask.sh: prompt is empty or whitespace only$ ./ask.sh "Summarize the diff"claude stub: args=[-p] stdin_bytes=18$ echo "Review this file" | ./ask.shclaude stub: args=[-p] stdin_bytes=16The third line is the common accident. The file is missing, cat fails, 2>/dev/null hides it, and without a check the empty value goes straight to claude -p. All three empty cases ended with exit code 2; only the two with text reached claude -p on standard input.
[5] Unattended jobs such as cron or CI often build the prompt from a file or an earlier step's output. If that step fails, the empty value still flows on, so keep the check above and treat exit code 2 as a job failure. To get the answer as JSON and detect failures, see Get claude -p output as JSON and use it in a script.