Created
May 6, 2026 18:51
-
-
Save siddolo/7330aed595b69cb52433ef6c3f69ea58 to your computer and use it in GitHub Desktop.
Simple Codex login shell wrapper: prompts go to Codex, !commands go to Bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -uo pipefail | |
| CODEX_BIN="${CODEX_BIN:-/home/sid/.nvm/versions/node/v22.12.0/bin/codex}" | |
| BASH_BIN="${BASH_BIN:-${SHELL:-/bin/bash}}" | |
| CODEX_SANDBOX="${CODEX_SANDBOX:-danger-full-access}" | |
| CODEX_OUTPUT_MODE="${CODEX_OUTPUT_MODE:-quiet}" | |
| CODEX_NAME="${CODEX_NAME:-hal9000}" | |
| CODEX_BASE_PROMPT="${CODEX_BASE_PROMPT:-\\[\\e[32m\\]\\w\\[\\e[0m\\] \\[\\e[36m\\]\\$\\[\\e[0m\\] }" | |
| if [[ ! -x "$CODEX_BIN" ]]; then | |
| printf 'error: codex binary not found or not executable: %s\n' "$CODEX_BIN" >&2 | |
| exit 1 | |
| fi | |
| if [[ ! -x "$BASH_BIN" ]]; then | |
| printf 'error: bash binary not found or not executable: %s\n' "$BASH_BIN" >&2 | |
| exit 1 | |
| fi | |
| _interrupted=0 | |
| handle_sigint() { | |
| _interrupted=1 | |
| printf '\n' | |
| } | |
| trim_leading_spaces() { | |
| local value="$1" | |
| value="${value#"${value%%[![:space:]]*}"}" | |
| printf '%s' "$value" | |
| } | |
| expand_ps1() { | |
| local prompt_source="$CODEX_BASE_PROMPT" | |
| local host short_host cwd dollar_char rl_start rl_end | |
| host="$(hostname 2>/dev/null || printf 'host')" | |
| short_host="${host%%.*}" | |
| cwd="${PWD/#${HOME:-}/\~}" | |
| rl_start=$'\001' | |
| rl_end=$'\002' | |
| if [[ ${EUID:-$(id -u)} -eq 0 ]]; then | |
| dollar_char="#" | |
| else | |
| dollar_char="$" | |
| fi | |
| prompt_source="${prompt_source//\\u/${USER:-user}}" | |
| prompt_source="${prompt_source//\\h/$short_host}" | |
| prompt_source="${prompt_source//\\H/$host}" | |
| prompt_source="${prompt_source//\\w/$cwd}" | |
| prompt_source="${prompt_source//\\W/${PWD##*/}}" | |
| prompt_source="${prompt_source//\\\$/$dollar_char}" | |
| prompt_source="${prompt_source//\\n/$'\n'}" | |
| prompt_source="${prompt_source//\\r/$'\r'}" | |
| prompt_source="${prompt_source//\\a/$'\a'}" | |
| prompt_source="${prompt_source//\\e/$'\e'}" | |
| prompt_source="${prompt_source//\\[/$rl_start}" | |
| prompt_source="${prompt_source//\\]/$rl_end}" | |
| printf '%s @ %s' "$CODEX_NAME" "$prompt_source" | |
| } | |
| run_bash_builtin() { | |
| local command="$1" | |
| local trimmed | |
| trimmed="$(trim_leading_spaces "$command")" | |
| if [[ "$trimmed" == cd || "$trimmed" == cd\ * ]]; then | |
| local target="${trimmed#cd}" | |
| target="$(trim_leading_spaces "$target")" | |
| if [[ -z "$target" ]]; then | |
| target="${HOME:-/}" | |
| fi | |
| if ! builtin cd -- "$target"; then | |
| return 1 | |
| fi | |
| return 0 | |
| fi | |
| if [[ "$trimmed" == pwd ]]; then | |
| pwd | |
| return 0 | |
| fi | |
| "$BASH_BIN" -lc "$command" | |
| } | |
| run_codex_exec() { | |
| local prompt="$1" | |
| if [[ "$CODEX_OUTPUT_MODE" == "quiet" ]] && command -v jq >/dev/null 2>&1; then | |
| "$CODEX_BIN" exec \ | |
| --json \ | |
| --skip-git-repo-check \ | |
| --sandbox "$CODEX_SANDBOX" \ | |
| -C "$PWD" \ | |
| "$prompt" | jq -r ' | |
| select(.type == "item.completed" and .item.type == "agent_message") | |
| | .item.text | |
| ' | |
| return | |
| fi | |
| "$CODEX_BIN" exec \ | |
| --skip-git-repo-check \ | |
| --sandbox "$CODEX_SANDBOX" \ | |
| -C "$PWD" \ | |
| "$prompt" | |
| } | |
| main() { | |
| local line bang_command prompt | |
| trap handle_sigint INT | |
| while true; do | |
| _interrupted=0 | |
| prompt="$(expand_ps1)" | |
| if ! IFS= read -r -e -p "$prompt" line; then | |
| printf '\n' | |
| break | |
| fi | |
| if (( _interrupted )); then | |
| continue | |
| fi | |
| if [[ -z "${line//[[:space:]]/}" ]]; then | |
| continue | |
| fi | |
| case "$line" in | |
| exit|logout) | |
| break | |
| ;; | |
| \!*) | |
| bang_command="${line:1}" | |
| if [[ -z "${bang_command//[[:space:]]/}" ]]; then | |
| continue | |
| fi | |
| run_bash_builtin "$bang_command" | |
| ;; | |
| *) | |
| run_codex_exec "$line" | |
| ;; | |
| esac | |
| done | |
| } | |
| main "$@" |
Author
siddolo
commented
May 6, 2026
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment