Score:1

bad variable name error on WSL

in flag

A similar question has been asked with no satisfying answer. I installed kaldi on WSL and when running a script I get this error, that apparantely has to do with "Program Files".

sh: 1: export: Files/WindowsApps/CanonicalGroupLimited.UbuntuonWindows_2004.2021.222.0_x64__79rhkp1fndgsc:/mnt/c/ProgramData/Oracle/Java/javapath:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/:/mnt/c/WINDOWS/System32/OpenSSH/:/mnt/c/Program: bad variable name

One of the answers was to make a link "Program" to "Program Files". But, it fails: /mnt/c$ sudo ln -s /mnt/c/"Program Files" Program ln: failed to create symbolic link 'Program' Making the link on the windows side succeeds but has no effect.

user535733 avatar
cn flag
Does this answer your question? [Why I obtain this error when I try to perform this simple bash script?](https://askubuntu.com/questions/506415/why-i-obtain-this-error-when-i-try-to-perform-this-simple-bash-script)
NotTheDr01ds avatar
vn flag
@user535733 This does not look like a duplicate of that question *at all*
muru avatar
us flag
@NotTheDr01ds you sure? Aside from the actual path used, this error is pretty much duplicated there.
NotTheDr01ds avatar
vn flag
@muru I've seriously thought about changing my profile to read "I've been using Linux 4 times longer than muru, but muru knows 4 times more than I do about it." (i.e. Muru, you rock!). But I'm fairly sure in this case that the linked question is similar in error only. That one was caused by an obviously bad use of the `export` command. I could be wrong, but I'm pretty dang certain this is a variable quoting issue that is causing the `export` to fail. I'm sure we can find another question that might be a better "duplicate" candidate, but the WSL aspects also make this one a bit more "special".
NotTheDr01ds avatar
vn flag
Seems to really be an almost exact duplicate of [this one](https://askubuntu.com/q/1215961/1165986), but there's really no good answer on that one either. Since that OP abandoned WSL for "pure" Ubuntu, I don't see them ever accepting an answer (they pretty much said so in a comment). For that reason, I'd propose that we close the earlier question in favor of this one (or find another duplicate). It's not usual, but I've seen instances across Stack sites where it was done for reasons like this.
Greenonline avatar
us flag
Please [edit] and provide the link to the 'similar question' to you refer to in your question
Score:1
vn flag

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.

NotTheDr01ds avatar
vn flag
@steeldriver I was thinking that, but then I figured the mangled PATH might still occur in `~/.bashrc` and then, since it's exported, would be picked up by the script in question (which is likely running under a `sh` shebang). That said, the more I think about it, the more I believe the quoting problem probably lies within the script in question.
NotTheDr01ds avatar
vn flag
@steeldriver Yes, I do mention that Windows *appends* the Windows path in the question, but there's also some *prepending* going on for some reason -- `/mnt/c/Program Files/WindowsApps/CanonicalGroupLimited.UbuntuonWindows_2004.2021.222.0_x64__79rhkp1fndgsc` is getting prepended, and that's unusual in WSL. You are right that it shouldn't be WSL that is doing this part.
NotTheDr01ds avatar
vn flag
@steeldriver I see the confusion - My statement was a bit ambiguous. When I said "prepending the Windows path to the actual Ubuntu UWP app", I meant that app path (not the "Windows path"). Edited to try to remove some of the ambiguity.
NotTheDr01ds avatar
vn flag
@steeldriver Oops - I was thinking that the first path was prepended, but now I see that the entire error is part of the *appended* path (including the UWP path). I'll edit my answer to remove that part. Thanks!
hr flag
Yeah it's getting tokenized as `export PATH=everything-up-to-first-whitespace everything-up-to-next-whitespace` which is equivalent to `export PATH=everything-up-to-first-whitespace` followed by `export everything-up-to-next-whitespace`. Another solution is simply to make sure `$PATH` is quoted on the RHS of the assignment.
NotTheDr01ds avatar
vn flag
@steeldriver *"Another solution is simply to make sure $PATH is quoted on the RHS of the assignment."* That's exactly the solution, I believe. Just have to find the offending line and fix it, which will be up to the OP to do.
hr flag
BTW your observation about `sh` versus `bash` is spot on - see [When is double-quoting necessary?](https://unix.stackexchange.com/a/68748/65304), in particular the section about the RHS of an assignment in **Where you can omit the double quotes** *Note that you do need the double quotes after `export`, because it's an ordinary builtin, not a keyword. This is only true in some shells such as dash, zsh (in sh emulation), yash or posh; bash and ksh both treat export specially.*
Score:0
aq flag

I may be a little late to the game here, but posting this in case anyone else stumbles across this thread. I was having this same issue with a fresh install of WSL using Ubuntu, and it was fixed by simply running these commands (as root) in WSL:

$ sudo apt update && sudo apt upgrade -y

All I had to do was restart my WSL terminal after this and the issue disappeared. Hope this helps someone!

Score:-1
in flag

I found the answer in https://arstechnica.com/civis/viewtopic.php?f=16&t=1472885 You need to remove the windows paths from $PATH, and you do that by creating /etc/wsl.conf with the following text: [interop] appendWindowsPath=False Then close the WSL window, wait 8 sec, and relauch.

NotTheDr01ds avatar
vn flag
Yes, I mentioned that being part of the problem in the first paragraph of my answer yesterday, and gave the `wsl.conf` option as a workaround, so you should accept that answer. Also note that removing the Windows path from WSL will reduce its functionality and usefulness, so it's recommended that you correct the quoting error in the script instead.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.