Bashrc doesn't export variable to function when changing directories

I have several projects that I am working on. Instead of setting aliases for each project location, I would rather set it when I go to that specific directory. Each project is a git repository, and I already have a mechanism that adds the current branch name to $ PS1 when I go to it.

In my .bashrc, I call the function: parse_git_branch to add to the command line:

case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h:\w\a\]$PS1\$(parse_git_branch) "
    ;;  
*)
;;  
esac

      

parse_git_branch is defined at the end of my .basrhc:

function parse_git_branch {
    GIT_BRANCH=$(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ \[\1\]/')
    if [[ -n $GIT_BRANCH ]];then
            source $HOME/bin/cur
    fi
    echo $GIT_BRANCH
}

      

if GIT_BRANCH is not empty, I run a simple script ~ / bin / cur that sets an alias for pwd:

#!/bin/bash
echo "got here!"
shopt -s expand_aliases
alias current="cd $(pwd)"

      

When searching for a source, no alias is set. The debug message is indeed displayed correctly.

I believe this is because shell scripts are run "outside" from my current environment. The alias is set when cur is called from the command line.

So why isn't the alias set when called from parse_git_branch inside my .bashrc?

Thank!

+3


source to share


2 answers


The problem here is using $ (), which runs parse_git_branch in a subshell. As a result, the current alias was not found on the open terminal because the aliases are not visible to parent processes.

case "$TERM" in
  xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h:\w\a\]$PS1\$(parse_git_branch) "
    source $HOME/bin/cur
    ;;  
*)
;;

      



must work.

+2


source


Ok I figured it out. The problem here is not why I am trying to do this. The problem is that I am trying to influence the parent shell from the subnetting environment spawned from it (says $ () syntax syntax). So the workaround was to allow communication from both environments. For this, I just used the filesystem. Here's what I did:

.bashrc didn't have to change much, but it didn't need to fix anything either:

parse_git_branch {
  GIT_BRANCH=$(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ \[\1\]/')
  if [[ -n $GIT_BRANCH ]];then
        $HOME/bin/cur -p
  fi
  echo $GIT_BRANCH
}

      

I modified ~ / bin / cur to just write or read fs and not try to change any environments:

#!/bin/bash
put() {
  echo $(pwd) > $HOME/.tmpalias
}
get() {
  cat $HOME/.tmpalias
}

case "$1" in
 *p) 
   put
   ;;
 *g) 
   get 
   ;;  
esac

      



The cur script will either write to or read from the .tmpalias file when called (I didn't bother with arguments and files, etc., since this is only called in two places.

Then I added the following to the .bash_aliases file:

alias current='cd $(cur -g)'

      

Voila! So I can expand this into an event capture mechanism that can be called on any command I run.

0


source







All Articles