0

When I run bash.exe in Git for Windows from WSL bash, I want to pass PATH from WSL to Win32, and filter out //wsl$/*** items in Win32 bash scripts. So I tried echo ${PATH//\/\/wsl\$+([^:]):/} but it takes several seconds to run.

My Win 10 is v1903 x64, WSL bash is from Ubuntu, and Git for Windows is v2.22.0.windows.1 . My CPU is Intel E3-1230 v5 .

The below is how I get a $PATH including //wsl$ :

  • open WSL bash, modify $PATH and export it
  • run WSLENV=PATH/l start2.exe /d/Git/usr/bin/bash.exe to get a new console window of bash.exe in Git for Windows
    • start2.exe is a tool of mine just to run exe in another console
  • echo $PATH shows something like /mingw64/bin:/usr/bin:/d/Program Files/nodejs://wsl$/Ubuntu/mingw64/bin:...

But in my computer, access to //wsl$/Ubuntu/*** is denied after once system upgrading. So I need to remove these //wsl$/* items from $PATH, otherwise the bash.exe seems to run slowly.

I tried export PATH="$(echo "$PATH" | sed -r 's|//wsl[^:]+:||g')" and it worked, but I want to use built-in commands to do so (the code below has been edited):

shopt -s extglob
if [[ $PATH == *//wsl\$* ]]; then
  PATH=${PATH//\/\/wsl\$+([^:]):/} # this line takes seconds
fi

I expect it runs quickly, but the code above is very very slow.

Added:

The main problem is my PATH is dynamic in WSL bash, and can even be changed by myself, so I can not pre-set a constant PATH for Git for Windows's bash.

Dahan Gong
  • 182
  • 8
  • why not have a separate redefinition of `PATH=/stuff/I/do/want:/in/path:...:...:. If your system has an `/etc/profile` put it in there and see if that solves your problem. Else put in a separate file and source as soon as possible from one of the `.rc` files that your system loads – shellter Aug 15 '19 at 13:49
  • The main problem is my PATH is dynamic in WSL bash, and can even be changed by myself, so I can not pre-set a constant PATH for Git for Windows's bash. – Dahan Gong Aug 16 '19 at 03:09
  • extglobs use a pattern-matching implementation built into bash. Consider using `[[ $string =~ $re ]]` instead, as it uses the standard C library's regex routines. One needs to use a fair bit of [actual computer science](https://swtch.com/~rsc/regexp/regexp1.html) to get a genuinely fast engine. – Charles Duffy Aug 16 '19 at 03:14
  • ...see https://stackoverflow.com/a/22261454/14122 for an example of using `BASH_REMATCH` for replacement as opposed to just in-place modification. – Charles Duffy Aug 16 '19 at 03:16
  • Thanks but I've seen it. The `[[ $PATH == *//wsl\$* ]]` is fast enough, and I just forgot to enable extglob before it in the example code. My issue is the string replacement of `${PATH//.../}`runs too slow. – Dahan Gong Aug 16 '19 at 03:24

0 Answers0