如何在 msys2 上的路径中转义 space?

How to escape a space in a path on msys2?

我想调用 C:\Program Files (x86)\Leela\Leela0100.exe 并传递一个文件名:

#!/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

但是,在 MSYS2 下通过 bash -x toleela.sh 调用我的脚本会产生错误:

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

正如已经在 IRC 上向您指出的那样,您在脚本的最后一行(以及其他行)缺少围绕变量扩展的引号。

"$LEELA" "$SGF"

您的可执行文件路径中的双正斜杠 (//) 不正确。请改用单正斜杠 (/) 或双反斜杠 (\)。编辑:反斜杠更复杂,所以坚持使用正斜杠。