WSL: Spaces in windows path breaks in Bash

Brief description

Using windows tools from Bash fails when using a path that has a space.

The windows path is added to the $PATH and some translation happens ie C: => /mnt/c. Spaces are not managed in this translation. This means any windows path that includes a space (Program Files or user paths with a space as below) fail to find files.

Expected results

Files found and are accessible.

Actual results

username@DESKTOP-M7UD40T:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mnt/c/Windows/System32:/mnt/c/Windows:/mnt/c/Windows/System32/wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0:/mnt/c/Program Files/nodejs:/mnt/c/Program Files/Microsoft SQL Server/130/Tools/Binn:/mnt/c/Program Files/dotnet:/mnt/c/Program Files/Git/cmd:/mnt/c/Users/User Name/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/User Name/AppData/Local/atom/bin:/mnt/c/Users/User Name/AppData/Roaming/npm
username@DESKTOP-M7UD40T:~$ apm
/mnt/c/Users/User Name/AppData/Local/atom/bin/../app-1.14.4/resources/cli/../app/apm/bin/apm: line 37: /mnt/c/Users/User Name/AppData/Local/atom/app-1.14.4/resources/app/apm/bin/node: No such file or directory
username@DESKTOP-M7UD40T:~$ ls /mnt/c/Users/User Name/AppData/Local/atom/app-1.14.4/resources/app/apm/bin
ls: cannot access 'Name/AppData/Local/atom/app-1.14.4/resources/app/apm/bin': No such file or directory
/mnt/c/Users/User
username@DESKTOP-M7UD40T:~$ ls "/mnt/c/Users/User Name/AppData/Local/atom/app-1.14.4/resources/app/apm/bin"
apm  apm.cmd  etc  node.exe  npm  npm.cmd  python-interceptor.sh
username@DESKTOP-M7UD40T:~$

Windows build number

Microsoft Windows [Version 10.0.15042]

Repro Steps Install Atom Editor & node.js in windows land open bash shell run apm command

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Reactions: 7
  • Comments: 33 (5 by maintainers)

Commits related to this issue

Most upvoted comments

I fixed the error, by not adding the Windows Path entries to the Linux Path. Open a wsl terminal, create or edit the wsl.conf file under /etc with the following command

sudo nano /etc/wsl.conf

Add the following content:

[interop]
appendWindowsPath = false

Save the file and restart the wsl. I used the following command to shutdown the wsl (closing the terminal window and open it again didn’t work)

wsl.exe --shutdown

Start a new wsl termial and check if the Windows Path entries are gone

echo $PATH

Output should look like

/home/[username]/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

I don’t know why anyone here faults this to the user. In Windows paths don’t need to be escaped, that’s why no program out there escapes their path they add.

xxx@PC:~$ $PATH -bash: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mnt/c/Python38/Scripts/:/mnt/c/Python38/:/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: No such file or directory

Notice how everything after “Program Files” is cut off?

How is it not WSL’s bug when it automatically adds Windows paths which work on Windows like they are, to the bash in a way that breaks them?

What is the user supposed to do, modify all their Windows paths? And do it again any time some program adds their path?

@emrsmsrli this isn’t all that useful when it’s the automatically generated path (Ubuntu + Windows) that has the spaces. I don’t plan on typing /mnt/c/some\ long\ path\app.exe every time I want to run something that’s already in the path.

My envisioned fix is that the auto combine process is updated to escape spaces exactly like that though. It’s not just “User Name” paths that will see this issue but also anything in Program Files.

you can use escape character \ before a space character as in "C:\Users\Emre\ Simsirli".

Hi, The solution to this is to double-quote the string eg:

$ export BROWSER="/mnt/c/Program Files/Firefox/firefox.exe"
$ "$BROWSER"

Found here: https://stackoverflow.com/questions/40135502/opening-sublime-text-from-bash-on-ubuntu-on-windows

I’m quite familiar with quoting and escape characters in nix. It simply does not work when passing the $BROWSER variable from bash to sensible-browser. In .bashrc, I tried single quote, double quotes, escape characters and even double quote embedded in single quote: export BROWSER="/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe" export BROWSER='/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe' export BROWSER='"/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe"' export BROWSER="/mnt/c/Program\ Files\ (x86)/Google/Chrome/Application/chrome.exe"

All produce the error: $ source ~/.bashrc $ /usr/bin/sensible-browser /usr/bin/sensible-browser: 6: /usr/bin/sensible-browser: /mnt/c/Program: not found

The only fix so far: In file /usr/bin/sensible-browser, update line that reads ${BROWSER} "$1" to "${BROWSER}" "$1"

This is definitely a bug and should be fixed.

This is not a bug.

I assume he means “bug in /usr/bin/sensible-browser”. There is, needless to say, no bug in WSL or Linux. Looks like you could report the bug here. I’d tell you where to to upstream a patch but the upstream Debian git link 404s.

I’m quite familiar with quoting and escape characters in nix

Then you know you can’t fix this with a different $BROWSER shell variable (f.e. in .bashrc). Because, the $BROWSER variable already contains the correct path if you quote it.

Good point re bug in sensible-browser. Will report there.

This is definitely a bug and should be fixed. To fix manually:

In file /usr/bin/sensible-browser, update line that reads ${BROWSER} "$1" to "${BROWSER}" "$1"

$ export BROWSER="/mnt/c/Program Files/Firefox/firefox.exe"
$ "$BROWSER"

Hey, there. I stucked with same problem.

SoleSensei@SoleSurface:~$ $BROWSER
-bash: /mnt/c/Program: No such file or directory
SoleSensei@SoleSurface:~$ export BROWSER='"/mnt/c/Program\ Files/Firefox/firefox.exe"'
SoleSensei@SoleSurface:~$ $BROWSER
-bash: "/mnt/c/Program\: No such file or directory
SoleSensei@SoleSurface:~$ export BROWSER="/mnt/c/Program\ Files/Firefox/firefox.exe"
SoleSensei@SoleSurface:~$ $BROWSER
-bash: /mnt/c/Program\: No such file or directory
SoleSensei@SoleSurface:~$ export BROWSER=/mnt/c/Program\ Files/Firefox/firefox.exe
SoleSensei@SoleSurface:~$ $BROWSER
-bash: /mnt/c/Program: No such file or directory
SoleSensei@SoleSurface:~$ export BROWSER='/mnt/c/Program Files/Firefox/firefox.exe'
SoleSensei@SoleSurface:~$ $BROWSER
-bash: /mnt/c/Program: No such file or directory
SoleSensei@SoleSurface:~$ xdg-open https://github.com
/usr/bin/xdg-open: 851: /usr/bin/xdg-open: /mnt/c/Program: not found
/usr/bin/xdg-open: 851: /usr/bin/xdg-open: /mnt/c/Program: not found
xdg-open: no method available for opening 'https://github.com'

Path just breaks on first space. Any ideas?

Hi @themaninthesuitcase – just for completeness, could you try running apm.exe (including the .exe)? WSL requires the file extension.