How can I use the arrow sign in my bash prompt?
You can use bash
’s PROMPT_COMMAND
to run a function which builds your prompt, e.g.:
PROMPT_COMMAND=build_prompt
build_prompt() {
EXIT=$? # save exit code of last command
red='\[\e[0;31m\]' # colors
green='\[\e[0;32m\]'
cyan='\[\e[1;36m\]'
reset='\[\e[0m\]'
PS1='${debian_chroot:+($debian_chroot)}' # begin prompt
if [ $EXIT != 0 ]; then # add arrow color dependent on exit code
PS1+="$red"
else
PS1+="$green"
fi
PS1+="→$reset $cyan\w$reset \\$ " # construct rest of prompt
}
Add this code to your ~/.bashrc
file and open a new terminal or run . ~/.bashrc
in an existing one for the changes to take effect. Note that I added the usual \$
at the end, this prints $
normally and #
if you’re root, thus preventing you from running commands as root unwittingly. The false
command is a good way to test the non-zero exit code variant:
If you’re into prompt themeing you should definitely take a look at the zsh
shell (package zsh
), whose famous configuration framework Oh My Zsh alone comes with over hundred themes. Additionally there are many other plugins available, for example the Spaceship ZSH prompt.
Links
- How can I shorten my command line (bash) prompt?
- Bash Prompt with Last Exit Code
- Easy Bash PS1 Generator
- Bash tips: Colors and formatting
- What color codes can I use in my PS1 prompt?
- What does "${debian_chroot:+($debian_chroot)}" do in my terminal prompt?