Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the following in my .bash_profile, to give me git branch info in my bash prompt:

# Configure colors, if available.
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
  c_reset='\[\e[0m\]'
  c_user='\[\033[1;33m\]'
  c_path='\[\e[0;33m\]'
  c_git_clean='\[\e[0;36m\]'
  c_git_dirty='\[\e[0;35m\]'
else
  c_reset=
  c_user=
  c_path=
  c_git_clean=
  c_git_dirty=
fi

# Function to assemble the Git part of our prompt.
git_prompt ()
{
  if ! git rev-parse --git-dir > /dev/null 2>&1; then
    return 0
  fi

  git_branch=$(git branch 2>/dev/null| sed -n '/^\*/s/^\* //p')

  if git diff --quiet 2>/dev/null >&2; then
    git_color="$c_git_clean"
  else
    git_color="$c_git_dirty"
  fi

  echo " [$git_color$git_branch${c_reset}]"
}

# Thy holy prompt.
PROMPT_COMMAND='PS1="${c_user}\u${c_reset}@${c_user}\h${c_reset}:${c_path}\w${c_reset}$(git_prompt)\$ "'

Everything is working great apart from the colours. If I remove the if statement around the colour definitions (and remove the else section) then I do get the colours.

I'm new to bash, I have little concept of what the if statement does and why it is not evaluating to true.

I'm happy to go without the if statement, but I assume it's there for a reason and until I know why then I don't want to cause myself unknown hassles.

I am running FreeBSD 9.0, and Bash 4.1.9(0)-release

share|improve this question

According to the manpage tput returns 1 if the terminal has the attribute.

So I think the check is inverted.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .