Running codex exec with a prompt is the quickest way to start a one-off non-interactive Codex task from the terminal. It works well for short repository questions, scripted checks, and CI steps that should finish and exit without opening the interactive TUI. These examples assume the Codex CLI is already installed and authenticated.

The prompt can be passed as the final command-line argument, or read from stdin when you omit the prompt or use a single hyphen as the placeholder. That makes it easy to use short inline instructions for quick runs and switch to piped or file-based prompts when the text gets longer.

Inline prompts can end up in shell history, so keep secrets, tokens, and other sensitive text off the command line. Use stdin or input redirection instead when the prompt is long, multiline, or confidential.

Steps to run Codex exec with a prompt:

  1. Run codex exec with a quoted inline prompt.
    $ codex exec "Reply with exactly OK."
    OK

    The quoted text after exec is the prompt. Quoting keeps spaces and punctuation in one shell argument instead of splitting them across multiple arguments.

    Add --sandbox read-only when the run should inspect files or answer questions without making changes.

  2. Use -C when the prompt should inspect a different directory than your current shell location.
    $ codex exec -C /home/user/projects/exec-demo --skip-git-repo-check "Reply with only the first heading from README.md."
    # Exec demo

    -C changes the working directory for that one run. Keep --skip-git-repo-check only when the target directory is not a Git repository.

  3. Feed a longer or multiline prompt through stdin by using a single hyphen as the prompt value.
    $ printf '%s\n' 'Reply with exactly STDIN-OK.' | codex exec -
    STDIN-OK

    Using stdin keeps long or sensitive prompts out of normal shell-history entries.

    If you pipe stdin while also supplying a prompt argument, Codex appends the piped content as a <stdin> block instead of replacing the prompt.

  4. Reuse a saved prompt file by redirecting it into codex exec.
    $ codex exec - < prompt.txt
    FILE-OK

    In this example, prompt.txt contains Reply with exactly FILE-OK.

    Input redirection is useful for reusable prompts in CI jobs, repository scripts, or prompt templates you want to edit separately from the shell command.