What if you just want to
run a single Claude prompt and get a response to send on? Or to pipe to another command in Linux? You can use the -p (print option)
Running Claude Code Programmatically
What do you do if you
need to run Claude Code on the command line to run just one prompt? Or you want it to output something and pipe
it to another command? You can use the
-p option see https://code.claude.com/docs/en/headless [1]
Let’s start very simple.
|
> claude -p "Hi!" |
That ran the prompt returned the results and exited.
Let’s try something more interesting.
|
> claude -p "Tell me a little bit
about Steve Wozniak" |
Now let’s try something that should fail (without some tweaks)
|
> claude -p "What is the current
temperature in Tokyo?" |
I did get a response but not the one I wanted. This is due to two things. 1. I need clearer instructions to tell it to use websites, and 2. Permissions! Its not allowed by default to pull from a web site.
Let me clear up the instructions first
|
> claude -p "What is the current
temperature in Tokyo? Use weather web sites to pull down this info" |
|
It
looks like the WebSearch tool hasn't been granted permission yet. You can
approve it in the permission prompt that should appear, or you can grant it
in your settings. Once approved, I'll be able to fetch the current Tokyo
temperature from a weather site. Would
you like to grant permission for web search, or would you prefer I try
fetching a specific weather URL directly instead? |
We can pass in permissions using the --alowedTools flag
|
> claude -p "What is the current temperature in Tokyo? Use weather web sites to pull down this info" --allowedTools "WebSearch,WebFetch" |
That got me the results I wanted!
Here are some current tools you can allow
Read – Read Files
Write – Create/overwrite files
Edit – make targeted edits to files
MultiEdit – Multiple edits in one go
Bash – Run shell commands
Glob – Find files by pattern
Grep – Search inside files
WebSearch – Search the web
WebFetch – Fetch and read a specific Webpage
LS – List Directory contents
You can use these generically (Which could be dangerous like
using bash. But you can also limit
them. For example.
|
> claude -p "What is the current stock price of NVDA? Use Yahoo Finance." --allowedTools "WebSearch,WebFetch(domain:finance.yahoo.com)" |
This will limit the Fetch to only getting data from finance.yahoo.com
--bare mode
If you do not need custom skills, MCP servers, or
project-specific claude.md instructions use the –bare option. It runs faster and cleaner.
|
> claude --bare -p "What is the current temperature in Tokyo? Use weather web sites to pull down this info" --allowedTools "WebSearch,WebFetch" |
That failed! Why?
|
> claude --bare -p "What is the
current temperature in Tokyo? Use weather web sites to pull down this
info" --allowedTools "Bash(curl
*)" |
I guess the answer for me was to allow Bash(curl *)
So fiddle with this and make sure it works for you.
Output formats
You can set the output format and even the JSON schema
|
> claude --bare -p "What is the current temperature in Tokyo? Use weather web sites to pull down this info" --allowedTools "Bash(curl *)" --output-format json | jq . |
This will give you a JSON result back that has the result in
the “result” field and contains all kinds of fun data back from claude. Like total_cost_usd and “modelUsage”
This could be very useful for diagnostics.
It shows command that failed due to permissions. In this case it tried to run curl (which it had permissions to run), but it also tried to run python3 which it did not have permissions to run
But it does not show which tools were used successfully…
Oh you can add a verbose!
|
> claude --bare -p "What is the current temperature in Tokyo? Use weather web sites to pull down this info" --allowedTools "Bash(curl *)" --output-format json --verbose | jq . |
That provides a lot… And I mean a lot more detail.
There is also a --json-schema option that allows you to shape the output. Let’s give it a try.
|
> claude --bare -p "What is the
current temperature in Tokyo? Use weather web sites to pull down this
info" --allowedTools "Bash(curl *)" --output-format json --json-schema '{
"type": "object", "properties": {
"city": { "type": "string" },
"temperature_celsius": { "type": "number" },
"condition": { "type": "string" },
"source": { "type": "string" } },
"required": ["city", "temperature_celsius",
"condition", "source"] }' | jq . |
It still outputs a big json file but it adds a structured_output section
To just get that section you could tweak the jq command slightly
|
> claude --bare -p "What is the
current temperature in Tokyo? Use weather web sites to pull down this
info" --allowedTools "Bash(curl *)" --output-format json
--json-schema '{ "type": "object",
"properties": { "city": { "type":
"string" }, "temperature_celsius": { "type":
"number" }, "condition": { "type":
"string" }, "source": { "type": "string"
} }, "required": ["city",
"temperature_celsius", "condition", "source"]
}' | jq
'.structured_output' |
References
[1] Run Claude Code
programmatically
https://code.claude.com/docs/en/headless
Accessed 05/2026
No comments:
Post a Comment