0

Full non-working code:

change_git=''

function cd() {
  change_git=''
  builtin cd "$@"
}

function git_info() {
  if [ -z $change_git]; then
    local st=$(bash -c "git symbolic-ref HEAD" 2> /dev/null)
    local bn=${st#refs/heads/}
    if [[ ! -z "${bn// }" ]]; then
      change_git="branch is: ${st#refs/heads/}"
    fi
  fi
  echo $change_git
}

PS1=$'Dir git status: $(git_info)'

This is what I am trying to achieve:

The git branch should be cached in the change_git variable until the directory is changed. However, that doesn't happen and the variable is calculated every time.

Since I would like to print this to prompt, this causes the prompt to be slow (hence my reason for caching). And yes, prompt is faster once I remove this code, and is hence how I know that it is executed everytime.

Also, this is in zsh. And yes, I am also using oh-my-zsh.

Finally, please do not ask me to replace bash -c "git symbolic-ref HEAD" with just git symbolic-ref HEAD. I am using WSL and sharing git from Ubuntu to my Windows; and I am using MSYS2 as main terminal.

Nissa
  • 4,636
  • 8
  • 29
  • 37
user3271166
  • 573
  • 1
  • 6
  • 17

2 Answers2

0

You have different ways to reach the behavior that you have described.

  • 1) share a global variable in your different functions but not the most sexy
  • 2) use the return code of your function to set/reset your myvar=x()
  • 3) you can also pass the variable by reference

http://www.fvue.nl/wiki/Bash:_Passing_variables_by_reference

Bash - Passing arguments by reference

Allan
  • 12,117
  • 3
  • 27
  • 51
  • Could you please demonstrate an example on how to use 1st option? I tried using `export` but failed. Its a string value so return code wouldn't work. 3rd option seems too complicated for my use case. – user3271166 Nov 07 '17 at 03:43
  • export could only be used to make the variable name available to any subshells forked from current shell. however, once the subshell have been forked. The subshell will have a new copy of the variable and the value, any changes to the exported variable in the shell will not effect the subshell. – Allan Nov 07 '17 at 04:13
  • Have a look at that link where all the variable declarations are defined http://www.thegeekstuff.com/2010/05/bash-variables – Allan Nov 07 '17 at 04:18
0

Your second version is very different from the first, because you're not echoing different things in the two cases.

When $myvar is empty, you do:

test=$myvar # sets $test to empty string
myvar='something'$myvar # sets $myvar to 'something'
echo `$myvar$test` # echoes 'something'.

When $myvar is not empty, you do:

echo $myvar$test # echoes 'something'

You never get somethingsomethingsomething because the only time you append $myvar is when $myvar is empty, and then you're appending an empty string. And $test is always an empty string because you're just copying the empty $myvar to it.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks (sorry for the unhelpful examples, I had a synapse failure scripting 2 am in morning). I figured out (or deducted) my problem is probably because I have commands in `.bashrc` so its probably something to do with commands being launched in subshell and variables not carrying on. The examples worked in normal scripts, but dont seem to work in my .bashrc for some reason (maybe I need to figure out something else). – user3271166 Nov 08 '17 at 02:17
  • `.bashrc` isn't run in a subshell, it's sourced in your shell. – Barmar Nov 08 '17 at 05:33
  • Maybe you should post the actual code, instead of all this "something" stuff. – Barmar Nov 08 '17 at 05:33
  • I have posted actual code (gonna loose grade for this since its part of my project, but I would like to know the answer, more importantly). Also, I just switched to zsh, same results though. – user3271166 Nov 09 '17 at 04:27
  • You need a space before `]` in `if [ -z $change_git]; then` – Barmar Nov 09 '17 at 16:52