列出目录内容无法使用变量

时间:2018-10-12 10:00:51

标签: bash shell terminal scripting

我正在尝试在终端中带有变量的路径上打印内容,但是不幸的是它对我不起作用。

输入

$ LX_PATH="/Users/pc/Library/Developer/Xcode/Archives/2018-10-10/App\ 10-10-18\,\ 11.16\ AM.xcarchive"
$ ls $LX_PATH

输出

ls: /Users/pc/Library/Developer/Xcode/Archives/2018-10-10/App\: No such file or directory
ls: 10-10-18\,\: No such file or directory
ls: 11.16\: No such file or directory
ls: AM.xcarchive: No such file or directory

但是当我尝试不使用下面的变量时!!

ls /Users/pc/Library/Developer/Xcode/Archives/2018-10-10/App\ 10-10-18\,\ 11.16\ AM.xcarchive

输出

BCSymbolMaps    Info.plist  Products    SwiftSupport    dSYMs

1 个答案:

答案 0 :(得分:2)

您正在“过度转义” LX_PATH的内容。定义LX_PATH时,使用的是双引号,这意味着不需要转义空格(但需要美元符号)。您还需要在ls中引用该变量,否则shell将尝试解析该变量并在空格处进行抱怨。尝试:

$ LX_PATH="/Users/pc/Library/Developer/Xcode/Archives/2018-10-10/App 10-10-18, 11.16 AM.xcarchive"
$ ls "$LX_PATH"