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 803347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:44:45+00:00 2026-05-14T23:44:45+00:00

I am using this function in Bash function parse_git_branch { git_status=$(git status 2> /dev/null)

  • 0

I am using this function in Bash

function parse_git_branch {
  git_status="$(git status 2> /dev/null)"
  pattern="^# On branch ([^${IFS}]*)"
  if [[ ! ${git_status}} =~ "working directory clean" ]]; then
    state="*"
  fi
  # add an else if or two here if you want to get more specific

  if [[ ${git_status} =~ ${pattern} ]]; then
    branch=${BASH_REMATCH[1]}
    echo "(${branch}${state})"
  fi
}

but I’m determined to use zsh. While I can use this perfectly as a shell script (even without a shebang) in my .zshrc the error is a parse error on this line if [[ ! ${git_status}}…

What do I need to do to get it ready for zshell?

Edit: The “actual error” I’m getting is " parse error near } and it refers to the line with the strange double }}, which works on Bash.

Edit: Here’s the final code, just for fun:

parse_git_branch() {
    git_status="$(git status 2> /dev/null)"
pattern="^# On branch ([^[:space:]]*)"
    if [[ ! ${git_status} =~ "working directory clean" ]]; then
        state="*"
    fi
    if [[ ${git_status} =~ ${pattern} ]]; then
      branch=${match[1]}
      echo "(${branch}${state})"
    fi
}

setopt PROMPT_SUBST
PROMPT='$PR_GREEN%n@$PR_GREEN%m%u$PR_NO_COLOR:$PR_BLUE%2c$PR_NO_COLOR%(!.#.$)'
RPROMPT='$PR_GREEN$(parse_git_branch)$PR_NO_COLOR'

Thanks to everybody for your patience and help.

Edit: The best answer has schooled us all: git status is porcelain (UI). Good scripting goes against GIT plumbing. Here’s the final function:

# The latest version of Chris' function below

PROMPT='$PR_GREEN%n@$PR_GREEN%m%u$PR_NO_COLOR:$PR_BLUE%2c$PR_NO_COLOR%(!.#.$)'
RPROMPT='$PR_GREEN$(parse_git_branch)$PR_NO_COLOR'

Note that only the prompt is zsh-specific. In Bash it would be your prompt plus "\$(parse_git_branch)".

This might be slower (more calls to GIT, but that’s an empirical question) but it won’t be broken by changes in GIT (they don’t change the plumbing). And that is very important for a good script moving forward.

  • 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-14T23:44:45+00:00Added an answer on May 14, 2026 at 11:44 pm

    You should really use Git “plumbing” commands to extract the info you want. The output from the “porcelain” commands (e.g.git status) may change over time, but the behavior of the “plumbing” commands is much more stable.

    With the porcelain interfaces, it can also be done without “bashisms” or “zshisms” (i.e. the =~ matching operator):

    parse_git_branch() {
        in_wd="$(git rev-parse --is-inside-work-tree 2>/dev/null)" || return
        test "$in_wd" = true || return
        state=''
        git update-index --refresh -q >/dev/null # avoid false positives with diff-index
        if git rev-parse --verify HEAD >/dev/null 2>&1; then
            git diff-index HEAD --quiet 2>/dev/null || state='*'
        else
            state='#'
        fi
        (
            d="$(git rev-parse --show-cdup)" &&
            cd "$d" &&
            test -z "$(git ls-files --others --exclude-standard .)"
        ) >/dev/null 2>&1 || state="${state}+"
        branch="$(git symbolic-ref HEAD 2>/dev/null)"
        test -z "$branch" && branch='<detached-HEAD>'
        echo "${branch#refs/heads/}${state}"
    }
    

    Integrating the output into the prompt is still shell specific (i.e. escape or quote the $ (for both bash and zsh) and set PROMPT_SUBST (for zsh)).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 395k
  • Answers 395k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Check your actual home directory. /home/robinjfisher to ensure its "other"… May 15, 2026 at 2:43 am
  • Editorial Team
    Editorial Team added an answer You might find this tutorial on FPA of interest. Personally,… May 15, 2026 at 2:43 am
  • Editorial Team
    Editorial Team added an answer I think what you need is Hierarchical Select May 15, 2026 at 2:43 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.