2

The problem

I use Git Bash which is based on MinGW. Around the day I often change the Windows PATH environment variable, in it are the paths to my tools, such as PHP, for example.

I use tools to change PHP versions, this tool automatically changes the PHP environment variable to a different version.

I would like to have a command that from the bash terminal itself I can run it and refresh the environment variables.

What have I tried?

I have tried almost everything in this question without any success. The only thing that has worked for me is to run a new terminal with administrator privileges, but I would not want to have this as the best option.

I tried:

I guess none of the above worked for me because they were not made for git bash, that's why I created this question. I have also tried all of the above with a terminal like Cygwin which is similar to Git Bash, without success.

I've come close to achieving this with this command, but I can't get it to work.

export PATH="$(cygpath -pu "`{ reg query 'HKEY_CURRENT_USER\Environment' /v PATH | grep PATH | cut -c23- ; reg query 'HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' /v PATH | grep PATH | cut -c30-; } | tr "\n" " "`")"

Basically what I am trying to achieve with the above command is to take the PATH environment variables out of the registry and put them in export PATH to update bash. But first I have to concatenate the system variables and the user variables. See this for more info.

4
  • 2
    I always avoid touching the system PATH in Windows. Instead I make changes to PATH inside the shell itself. This allows mo to have different instances of the bash shell doing different things (e.g. using different compiler versions). Commented Feb 16, 2022 at 23:05
  • Hi @BrechtSanders, can you give me an example of this? Commented Feb 16, 2022 at 23:09
  • 1
    For example (in bash): export PATH=D:/Perl/bin:$PATH Make sure to avoid spaces and backslashes Commented Feb 17, 2022 at 6:38
  • 1
    I use scripts (which I source) which add tool-chain version dependent folders to paths like variables, including PATH and also sets any dependent environment variables. My default setup has no tool-chain specialization. I can start multiple terminal windows or mintty windows, and customize each window by sourcing different scripts so I can test my code with multiple tool-chain versions. This technique works for cmd or terminal windows (using batch scripts) and for cygwin, mingw, git bash, and linux (using bash scripts). This is a safe and reliable method for setting up isolated setups. Commented Feb 17, 2022 at 17:32

1 Answer 1

2

I used a custom script created by Badr Elmers Works just as I expected and works in Git Bash, Cygwin.

More info here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.