skip to Main Content

I’m developing PHP on Windows machine and due to various old project I need to switch PHP version frequently. I wrote a script that edits $PATH environment variable and it works without a problem.

I’m using Git Bash as a CLI tools. All I need to refresh $PATH is to close the app and load it again. Simple enough, works well, php -v starts reporting correct version.

Problem is, I’m also using Git Bash integrated in Git Extensions and PhpStorm. Turning Bash off inside them doesn’t work. Neither does restarting the applications themselves. I’m forced to restart the PC, which is of course annoying.

Is there a way to force Bash to reload environment variables via code?

Answers I found suggest running

bash
source ~/.bashrc
source ~/.bash_profile

But none of those work.

In PowerShell working solution is this:

$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")

so I need something similar but suited for bash.

2

Answers


  1. Chosen as BEST ANSWER

    Writing it out jumpstarted my mind and I figured what I had to do.

    Since I already have a script that changes PATH in Windows, I can use the same script to edit files bash_profile and bashrc.

    In the end this is enough to make it work, first line is changed dynamically:

    PATH=/c/wamp64/bin/php/php5.6.40:$PATH
    export PATH
    alias reload='source ~/.bash_profile'
    

  2. Here is the way

    # Temporary environment variables
    $ export PATH=/you/can/set/git/path/here
    

    on windows it will be something like C:Usersyourpath

    #Confrm if its set 
    $ echo $PATH
    

    Also in Bash you can check what variables are there with $ env

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search