Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7513501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:12:16+00:00 2026-05-30T00:12:16+00:00

Currently my terminal prompt includes git information. Example clean directory: michaelespinosa:~/Sites/example.com [git:master] Example dirty

  • 0

Currently my terminal prompt includes git information.

Example clean directory: michaelespinosa:~/Sites/example.com [git:master]
Example dirty directory: michaelespinosa:~/Sites/example.com [git:master*]

I want also to color the git information (green if it’s clean or red if it’s dirty).

I thought I could add a function (parse_git_color) and set it’s color accordingly with an if else statement based of whether or not an asterisk is present.

The problem is that it keeps returning green for both clean and dirty directories. I believe the problem has something to do with the if statement parse_git_dirty == “*” that compares the value value of parse_git_dirty to “*”.

function parse_git_dirty {
  [[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "*"
}
function parse_git_color {
  if [ parse_git_dirty == "*" ]; then
    echo '\033[0;31m\'
  else
    echo '\033[0;32m\'
  fi
}
function parse_git_branch {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/$(parse_git_color)[git:\1$(parse_git_dirty)]/"
}

Any help is appreciated!
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T00:12:17+00:00Added an answer on May 30, 2026 at 12:12 am

    Please note the \$(...) in PS1. It is important because only with \$() your command is called every time your prompt is printed. $() is called only once (when PS1 was initialized).

    function parse_git_dirty
    {
      [[ "$(git status 2> /dev/null | tail -n1)" != "nothing to commit (working directory clean)" ]] && echo "*"
    }
    
    function parse_git_color
    {
      if [ "$(parse_git_dirty)" == "*" ] ; then
        echo "0;31m"
      else
        echo "0;32m"
      fi
    }
    
    function parse_git_branch
    {
      git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/[git:\1$(parse_git_dirty)]/"
    }
    
    export PS1="\[\033[\$(parse_git_color)\]\$(parse_git_branch) \[\033[0m\]"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently writing a very simple terminal tool and I want to implement the
i am currently trying to run a web crawler through the terminal. it compiles
I am writing a Python program which runs a virtual terminal. Currently I am
I want to get the size of the current terminal, i.e. the terminal my
Hi Thanks for reading my question. I currently use Mac Terminal to use MySQL.
I'm currently using the following code to output text in green to the terminal:
I want to make program to talk between a Bank and POS Terminal Devices
I've done something to break my Bash Shell Prompt in OS X (10.5.7) Terminal.
I currently have my ZSH PROMPT variable set up as: PROMPT=$'[%{\e[0;32m%}%n@%m:%~%{\e[0m%}]> and my RPROMPT
I'm currently writing a linux program that produces colored output on the terminal. Since

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.