Home > Enterprise >  Force reload PATH in bash
Force reload PATH in bash

Time:03-30

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.

CodePudding user response:

Here is the way

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

on windows it will be something like C:\Users\your\path

#Confrm if its set 
$ echo $PATH

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

CodePudding user response:

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'
  • Related