Skip to content

Instantly share code, notes, and snippets.

@acobster
Created January 18, 2017 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acobster/e830b261c3cc26786a90448fb9ef3dd9 to your computer and use it in GitHub Desktop.
Save acobster/e830b261c3cc26786a90448fb9ef3dd9 to your computer and use it in GitHub Desktop.
Customize bash prompt based on current cwd/environment
# You want your prompt to warn you if you are in the directory tree of a production environment.
# Set the `LIVE_DIRS` array to the list of whatever directories should be considered production,
# and your prompt will contain a big, red "[PRODUCTION]" whenever you're in one of those directories,
# or a subdirectory thereof.
# Customize this variable
LIVE_DIRS=(/var/www/example.com /var/www/example2.com)
cwdEnvironment() {
local _live
_live=''
for d in "${LIVE_DIRS[@]}" ; do
if [[ `pwd` =~ ^"$d".*$ ]] ; then
_live=' [PRODUCTION]'
fi
done
printf -- '%s' "$_live"
return 0
}
# The important part here is the `\[$(tput bold 1)\]\e[0:31m$(cwdEnvironment)\e[m\[$(tput sgr0)\]` - that's the bold, red part
export PS1='[\u@\h \W]\[$(tput bold 1)\]\e[0:31m$(cwdEnvironment)\e[m\[$(tput sgr0)\] \$ '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment