Search code examples
msys2

How to escape a space in a path on msys2?


I want to invoke C:\Program Files (x86)\Leela\Leela0100.exe and pass it a filename:

#!/bin/bash -x

BASE="c:/Users/supreme"
DROPBOX="$BASE/Dropbox"

DOWNLOADS="$BASE/Downloads"

cd $DOWNLOADS
SGF=`ls -th *.sgf | head -1`

GAMES="$DROPBOX/Baduk/Games"

mv $SGF $GAMES

LEELA='c://Program Files (x86)//Leela//Leela0100.exe'

cd $GAMES
$LEELA $SGF

However, invoking my script via bash -x toleela.sh under MSYS2 yields the error:

c:\Users\supreme\Dropbox\Programming>bash -x toleela.sh 
bash -x toleela.sh 
+ BASE=c:/Users/supreme
+ DROPBOX=c:/Users/supreme/Dropbox
+ DOWNLOADS=c:/Users/supreme/Downloads
+ cd c:/Users/supreme/Downloads
++ ls -th '9001447-269-Winggo-princepawn (1).sgf' 9429961-080-princepawn-RyanBLee.sgf
++ head -1
+ SGF=9429961-080-princepawn-RyanBLee.sgf
+ GAMES=c:/Users/supreme/Dropbox/Baduk/Games
+ mv 9429961-080-princepawn-RyanBLee.sgf c:/Users/supreme/Dropbox/Baduk/Games
+ LEELA='c://Program Files (x86)//Leela//Leela0100.exe'
+ cd c:/Users/supreme/Dropbox/Baduk/Games
+ c://Program Files '(x86)//Leela//Leela0100.exe' 9429961-080-princepawn-RyanBLee.sgf
toleela.sh: line 18: c://Program: No such file or directory

Solution

  • As was already pointed out to you on IRC, you are missing quotes around the variable expansions on the last line of your script (also on other lines).

    "$LEELA" "$SGF"
    

    The double forward slashes (//) in your executable path are not right. Use single forward slash (/) or a double backslash (\\) instead. EDIT: Backslashes are more complicated, so stick to forward ones.