bad variable name error on WSL

Solution 1:

I have a hunch that this is caused by something in your script or startup files (assuming ~/.bashrc or ~/.profile) not properly quoting the PATH variable. Because WSL appends the Windows path to the WSL path automatically, it's adding some path elements with "Program Files" in, which is correct.

But it does require proper variable quoting. Check your script for use of $PATH and quote it (or add the potentially offending lines to your question if you need help with that). If you don't see anything suspect in the script, then also check your ~/.bashrc and ~/.profile.

Edit/update: I'm fairly sure the offending line will be in the script which is being processed through dash (i.e. sh). Bash can handle this just fine without additional quoting:

> export PATH=$PATH:newpath
> echo $?
0
> echo $PATH
> # outputs correct PATH, even with spaces in the Windows path

However, run sh and try the same:

$ export PATH=$PATH:newpath
sh: 1: export: Files/NVIDIA: bad variable name
$ export PATH="$PATH":newpath
$ echo $?
0
$ echo $PATH
# outputs correct PATH, even with spaces in the Windows path

There's also a "bandaid" solution to disable the WSL feature that appends the Windows path to the WSL/Linux path. You can do this by creating (or editing if it already exists) /etc/wsl.conf and adding the following lines:

[interop]
appendWindowsPath=false

Then stop the instance with wsl --terminate Ubuntu (assuming the default distro name), and restart WSL.

It's not a good permanent solution, IMHO, since it makes it far more difficult to run Windows apps (such as VSCode) when they aren't in the path.

Better to figure out the core issue and fix it in the scripts.