Question

if i run this in command line with in git repo

git status 2> /dev/null | grep -iw 'modified'

the output i get

#   modified:   .rvmrc

so my assumption is that if i plug this in into if statement i will get true that will execute the line of code

but when i created function as part of the .bash_profile, and here is the code i have:

## Prompt ##
path="$white\w$reset"
folder_name="$white\W$reset"
git_branch="\$(git branch 2>/dev/null | grep -e '\* ' | sed 's/^..\(.*\)/$red(\1)$reset/')"
rvm="$green(\$(~/.rvm/bin/rvm-prompt i v p))$reset"

# Checks if working tree is dirty
function _git_st {
  if [[ $(git status 2> /dev/null | grep -iw 'modified')  ]]; then
    echo "[x]"
  fi
}
git_st="$yellow`_git_st`$reset"

PS1="$rvm $folder_name $git_branch $git_st \$ "

X is not echoed.... i am bit lost, and not sure what i am doing wrong.

this is the out put i get:

(ruby-1.9.2-p290) folder-git (master)  $ git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   .rvmrc
#
no changes added to commit (use "git add" and/or "git commit -a")
(ruby-1.9.2-p290) folder-git (master)  $ 
Was it helpful?

Solution

The reason for this is because you are using a variable git_st to set PS1 which will be evaluated at the time of setting PS1. What you can try is to invoke the function _git_st be called instead of using git_st i.e. try something on the lines of:
PS1="$rvm $folder_name $git_branch \$(_git_st) \$ ".
Also you might be interested to know that newer versions of git provide functions along with bash completion which provide such utilities. You can take a look at __git_ps1 function if it is available.
Hope this helps!
P.S.: Here is SO post which might provide some pointers for using __git_ps1. This is the quick search result from Google.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top