Running a single Codex prompt from the command line is ideal for quick, repeatable one-off tasks and for dropping an assistant response directly into scripts or tooling. The exec mode streams progress to stderr and prints the final message to stdout, which keeps automation predictable.
The codex exec command submits exactly one prompt to Codex in a non-interactive run. You can pass the prompt as an argument or use - to read it from stdin, which is useful when prompts are generated by other tools.
Because the prompt is passed via the shell, quoting and escaping affect what is actually sent. Command-line arguments can also end up in shell history and local process listings, so avoid placing secrets or sensitive data directly in the prompt string.
Related: How to run Codex exec with a command
Related: How to run Codex in full-auto mode
Steps to run Codex exec with a prompt:
- Run Codex exec with a short prompt.
$ codex exec "Return OK." OK
Prompt text provided on the command line can be stored in shell history and may be visible in local process listings; avoid including passwords, tokens, or other secrets.
- Supply a one-off API key for a CI run without a prior login.
$ CODEX_API_KEY="sk-..." codex exec "Return OK." OK
CODEX_API_KEY is supported only for codex exec.
- Add an explicit output constraint in the prompt when the response will be parsed by scripts.
$ codex exec "Return a single-line JSON object with key status and value ok." {"status":"ok"}Single-line output reduces parsing surprises in pipelines and log collectors.
- Read the prompt from stdin when the text comes from another command.
$ printf '%s' "Return OK." | codex exec - OK
Use - to read the prompt from standard input.
- Save the response to a file when the output will be reused later.
$ codex exec "Return OK." > /tmp/codex-ok.txt
Redirection captures standard output while leaving terminal messages visible.
- Save only the last assistant message when you want a clean output file.
$ codex exec --output-last-message /tmp/codex-last.txt "Return OK." OK
- Verify the saved file contains the expected response.
$ cat /tmp/codex-last.txt OK
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
