A shell command can produce more output than an automation job needs. With codex exec, Codex can run that command and extract the requested result before returning a final answer.

The prompt argument is an instruction for the agent, rather than an arbitrary shell-command slot. Naming the command and the required answer gives Codex both the operation to perform and a clear boundary for interpreting its output.

You need Bash or zsh, an installed Codex CLI with a configured model provider, and an existing Git repository. Repository files and terminal output can reach that provider, so the project should contain only data you can share with it.

Steps to run a command with Codex exec:

  1. Change to the Git repository where you want Codex to run the command.
    $ cd /home/user/project

    The path /home/user/project represents your own repository directory. The current directory determines where the shell command runs. A directory outside Git requires the repository-check override described in How to fix the Codex trusted-directory error.
    Related: How to set the working directory for Codex

  2. Ask Codex to run git rev-parse --show-toplevel through its shell tool.
    $ codex exec --ephemeral --sandbox read-only "Run git rev-parse --show-toplevel using the shell tool, then report the repository root. Do not change any files."

    This Git command prints the repository's root directory without changing files. The explicit --sandbox read-only option restricts file writes by the agent's shell commands. The --ephemeral option prevents session rollout files from being saved. Progress appears on stderr and the final reply appears on stdout, so both remain visible in the terminal.

  3. Confirm that the execution output and final reply show your repository's root directory.

    The progress must show an actual shell execution of git rev-parse --show-toplevel and its successful result. A reply containing a path without an execution entry does not show that the command ran. A failed execution, a different repository path, or a permission error means the requested run has not succeeded.

Share this guide