A clean, informative shell prompt for bash and zsh. Zero dependencies, just shell builtins. Drop it in and get a prompt that shows your last exit code, current git branch, and an abbreviated working directory.
- Last command exit code indicator: green
oon success, redxon failure - Current git branch, detected by reading
.git/HEADdirectly (nogitfork) - Abbreviated working path:
~/projects/myappbecomes~/p/myapp - Optional hostname that shows automatically over SSH
- Color output by default, plain mode for dumb terminals or logs
- Works in bash 3.2+ and zsh 5+
o ~/p/myapp (main) $ xxx ~/p/myapp (main) $
The left prompt shows a normal successful command. The right shows what appears after a failing command (exit code 1 or higher).
Add one line to your shell config (~/.bashrc for bash, ~/.zshrc for zsh):
source /path/to/prompt.shThen start a new shell or run source ~/.bashrc (or ~/.zshrc).
git clone https://github.com/cappy-dev/cross-shell-prompt.git
echo "source $(pwd)/cross-shell-prompt/prompt.sh" >> ~/.bashrcFor zsh, use ~/.zshrc instead.
./prompt.shPrints a sample prompt so you can see the format before installing.
All settings are environment variables. Set them before sourcing the script.
Set to 1 to always show the hostname (even outside SSH):
export CSP_SHOW_HOST=1
source /path/to/prompt.shThe hostname shows automatically when SSH_CONNECTION or SSH_TTY is set.
Override any color before sourcing. Values are ANSI escape strings.
| Variable | Default | Used for |
|---|---|---|
CSP_COLOR_DIR |
bold cyan | Working directory |
CSP_COLOR_HOST |
magenta | Hostname |
CSP_COLOR_GIT |
yellow | Git branch |
CSP_COLOR_OK |
green | Success indicator |
CSP_COLOR_FAIL |
red | Failure indicator |
CSP_COLOR_RESET |
reset | Color reset |
Example with custom colors:
export CSP_COLOR_DIR=$'\033[1;34m'
source /path/to/prompt.shCall csp_disable_color after sourcing:
source /path/to/prompt.sh
csp_disable_colorUseful for dumb terminals or when piping shell output to a log.
The prompt keeps the first letter of each intermediate directory but shows
the final directory in full. Your home directory is replaced with ~.
| Full path | Prompt path |
|---|---|
/home/user |
~ |
/home/user/projects/myapp |
~/p/myapp |
/etc/systemd/system |
/e/s/system |
/var/log/nginx |
/v/l/nginx |
/ |
/ |
This keeps long paths short without hiding the last directory, which is the one you usually care about.
The script walks up the directory tree until it finds a .git directory or
file. It then reads .git/HEAD directly to determine the branch name. This
avoids forking git on every prompt, which makes the prompt fast even in
large repos.
Detached HEAD state shows the short commit hash followed by ....
Tags show tag:NAME format.
Tested with:
- bash 3.2 (macOS default), 4.x, 5.x
- zsh 5.x
Not compatible with fish, tcsh, or other shells. The script uses PROMPT_COMMAND
(bash) or precmd_functions (zsh) hooks that only exist in those two shells.
prompt.sh- the full script. Source this file from your shell config.LICENSE- MIT license.
MIT. See LICENSE.