# How to Run Claude Code from the Command Line

> Run Claude Code from the command line with claude -p: install the CLI, run a one-shot prompt, pipe files, get JSON output and continue a session.

You can run Claude Code from the command line without opening an interactive session. Add `-p` (short for `--print`) to the `claude` command to use print mode, also called headless or non-interactive mode: Claude Code takes one prompt, prints the answer and exits. Use it in scripts, cron jobs or for a quick question.

## Install the Claude Code CLI

On macOS, Linux or WSL, run the installer:

```bash
curl -fsSL https://claude.ai/install.sh | bash
```

On Windows, run this in PowerShell:

```powershell
irm https://claude.ai/install.ps1 | iex
```

You can also install it with Homebrew:

```bash
brew install --cask claude-code
```

Or with npm:

```bash
npm install -g @anthropic-ai/claude-code
```

Then start Claude Code once and sign in in the browser. Claude Code needs a Pro, Max, Team or Enterprise plan, or a Claude Console account.

```bash
claude
```

## Run a prompt with claude -p

Put the prompt in quotes after `claude -p`:

```bash
claude -p "what is the purpose of life? answer one word"
```

Here is what Claude Code print mode looks like:

![Claude Code running claude -p with a one-word prompt in the terminal and printing the answer Love](/assets/en/blog/how-to-run-claude-code-from-command-line/claude-p-run-from-command-line.webp)

Claude Code prints only the answer, with no header, and returns you to the shell. Markdown is not rendered, so `**Love.**` is bold text in Markdown.

Interactive session or print mode:

![Diagram: need a back-and-forth chat? Run claude. Otherwise run claude -p with a prompt](/assets/en/blog/how-to-run-claude-code-from-command-line/diagram_task-claude-code.png)

## Useful claude -p options for scripts

**Send a file or command output to Claude.** Print mode reads piped text, and you can save the answer with `>`:

```bash
cat build-error.txt | claude -p "explain the root cause of this build error" > output.txt
```

How input and output flow:

![Diagram: a prompt and piped text go into claude -p, and the answer goes to the terminal, a file or JSON](/assets/en/blog/how-to-run-claude-code-from-command-line/diagram_prompt-piped-text.png)

**Get JSON for a script.** The answer is in the `result` field, next to the session id and cost:

```bash
claude -p "summarize this project" --output-format json
```

**Let Claude change files without asking.** List the tools it may use:

```bash
claude -p "run the tests and fix any failures" --allowedTools "Bash,Read,Edit"
```

**Allow file edits only:**

```bash
claude -p "apply the lint fixes" --permission-mode acceptEdits
```

**Continue the last conversation** with a follow-up prompt:

```bash
claude -p "now explain it in one sentence" --continue
```

For all flags, run `claude --help` or see the [Claude Code headless mode docs](https://code.claude.com/docs/en/headless).

## Related guides

- [How to Run Codex from the Command Line](/blog/how-to-run-codex-from-command-line/), the same task with `codex exec`.
- [Claude Code YOLO Mode: What The Flag Means](/blog/claude-code-yolo-mode-using-dangerously-skip-permissions/), before you turn off permission prompts.

## Frequently Asked Questions

### Can claude -p edit files?

Yes, if you allow it. In print mode nobody can answer permission prompts, so actions that need approval are denied. Add `--allowedTools "Read,Edit"` or `--permission-mode acceptEdits` to let Claude Code change files.

### How do I get only the text answer from JSON output?

Pipe the JSON into `jq` and read the `result` field: `claude -p "Summarize this project" --output-format json | jq -r '.result'`. The same JSON also holds the session id, which you can pass to `--resume` later.

### How can a script tell if claude -p failed?

Check the exit code. Claude Code exits with 0 on success and with a non-zero code when the run fails. An invalid flag is reported on stderr before the run starts.

---

[View the canonical page](https://revdoku.com/blog/how-to-run-claude-code-from-command-line/) · [Browse llms.txt](https://revdoku.com/llms.txt)
