1

I set a variable WORKSPACE="$(git rev-parse --show-toplevel)" in ~/.bashrc - which essentially sets $WORKSPACE to the root of the git repository so that I can write alias for some of the operations like building an image, going to my component directory in the repo etc.

Now I have created a bash shell script to create a new git worktree with a specified directory name and branch name. My script syntax would go like this - my_script -c <branch_name> -d <directory_name>

Now the challenge is once I create a new worktree the variable $WORKSPACE should be set to the newly created git worktree's directory name.

Currently I am doing -

cd $DIR -> which is `directory_name`
WORKSPACE="$(git rev-parse --show-toplevel)"

But this is not helping, as the set value of $WORKSPACE is for the non-interactive session where the script is running and not for the interactive bash shell (from where the script was triggered). So if I am triggering the alias to build an image, it is not triggered from the context of newly created worktree directory.

How can I address this issue?

3
  • You can export variables to the child process, but any changes the child process makes to its variables won't affect the value of the variables in the parent process. – Pourko Nov 15 '20 at 20:10
  • Yeah, understood. So is there a workaround to address this issue? Like putting the content of the script in the ~/.bashrc file as a function. Or when the script is run, making it to use the existing shell environment rather than creating a new one. Or making the script to initiate a new bash interactive shell and keeping it active even after the script does its job, so that future commands can be executed from there. Or any other workaround to address this issue? – Darshan L Nov 16 '20 at 3:30
  • @Asker321 I have provided the reason why that doesn't work in my scenario. – Darshan L Nov 16 '20 at 4:58
0

Run this one liner in the directory where you will get the new value of the git command : git rev-parse --show-toplevel > ~/tmpfile

Then add this in your .bashrc: WORKPLACE=$(cat ~/tmpfile)

Finally run source ~/.bashrc

Now you can reference WORKPLACE with the updated value anywhere in the filesystem.

You can manage the location of tmpfile at your ease but be sure to specify the location in .bashrc too, then.

2
  • This would make the $WORKSPACE to always point to the content of ~/tmpfile whenever I open a bash shell irrespective of which git repo I'm in. The point of having $WORKSPACE in my shell environment is to indicate inside which repo directory I'm in. Currently I've designed my ~/.bashrc to assign $WORKSPACE to hold the path of the git repo inside which I'm in. – Darshan L Nov 16 '20 at 4:48
  • When I open a bash shell, it sets the $WORKSPACE to the git repo I'm under. Then whatever custom commands i run like building an image or going to my component would work in the context of my current repo. In the case where I'm facing problem is when i need to create a new repo and build an image. I want something like this to work - my_script && build_image. my_script would create a new git worktree. If not exactly like the above two alias combination may be something similar in effort. – Darshan L Nov 16 '20 at 4:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.