Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Claude Workflow  ›  Capstone Project

Starting Claude from the Shell

claude 15 minPlain claude opens a conversation, claude with a quoted query opens one with a first message, claude -p prints a single answer for scripts, and --add-dir widens the workspace.

Starting Claude from the Shell

How you launch Claude Code decides what kind of session you get: a conversation, a conversation with a head start, or a single answer that a script can use. A few small variations on one command cover all three.

The rule

Plain claude to talk, add a query to start, -p to print and exit.

claude                                      # interactive session in this folder
claude "why does test_weekly_plan fail?"    # interactive, first prompt already sent
claude -p "summarise what orders.py does"   # print one answer, then exit
git diff --staged | claude -p "write a one-line commit message for this diff"
claude --add-dir ../tiffin-shared           # also work in a second folder
  • claude opens the familiar interactive session.
  • claude "query" does the same but sends your first message straight away, which is handy when you already know the question.
  • claude -p is print mode, often called headless mode. One prompt in, one answer out, then it exits. Because it writes to standard output and can read from a pipe, it fits into shell scripts, git hooks and scheduled jobs. Nobody is there to answer a permission prompt, so any tool that needs approval must be allowed in advance, or the call is refused.
  • --add-dir adds another folder to the session's workspace, such as a shared library that sits beside the project. Add only what the task needs.

claude --help lists the other options, such as choosing a model or a permission mode at launch. Print mode can also return structured output, such as JSON, for a program to parse.

One shell detail trips people up: put the query in double quotes so the shell passes it as a single argument. An unquoted apostrophe, as in don't, starts a quoted string of its own, and the shell will sit waiting for you to close it.

The shortcut

Ask "Will I reply?" If there will be back-and-forth, use claude, with a quoted query if you already know your opening question. If the answer goes into a file, a pipe or a script, use -p.

A day on tiffin, four launchesStep 1 of 4
  1. Morning investigation

    claude "last night's job logged three failed payments; find out why". You will want to follow up, so it is interactive, with a head start.

The trap is a scripted -p run with broad permissions. Nobody is watching to approve or stop it, so give unattended runs the narrowest permissions that still do the job.

Drills

Answer fast — trust the rule.

quick drill

Which command opens an interactive session with your first question already sent?

quick drill

Which command prints a single answer and exits, ready to use in a pipe?

quick drill

You pipe the staged diff into claude -p "write a commit message". What does Claude receive?

quick drill

The date helpers live in ../tiffin-shared. How do you let the session work there too?

Remember

  • claude to talk, claude "query" to start with a question, claude -p to print and exit.
  • Shortcut: will I reply? If not, use -p.
  • Trap: unattended -p runs with more permissions than they need.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhich launch?

For each tiffin situation, choose claude, claude "query", claude -p or claude --add-dir and explain why: (a) investigating last night's failed payments; (b) generating a commit message inside a script; (c) a change that must also edit the shared date helpers in ../tiffin-shared.