路径名中的转义空格



我正在尝试转义路径名中的空格 /Volumes/NO NAME .我需要通过bash脚本访问此目录中存在的文件。我使用了sedprintf命令,但似乎都没有奏效。到目前为止,我拥有的代码:

while read line
do
  if [[ -d ${line} ]]; then
    echo "${line} is a directory. Skipping this.";
  elif [[ -f ${line} ]]; then
    spacedOutLine=`echo ${line} | sed -e 's/ /\ /g'`;
    #spacedOutLine=$(printf %q "$line");
    echo "line = ${line} and spacedOutLine = ${spacedOutLine}";
  else
    echo "Some other type of file. Unrecognized.";
  fi
done < ${2}

这些似乎都没有奏效。对于类似这样的输入: /Volumes/NO NAME/hello.txt ,输出为:

/Volumes/NO: No such file or directory
NAME/hello.txt: No such file or directory  

我在Mac上并通过终端使用bash。我也浏览了很多关于SO的其他帖子,这些帖子对我没有帮助。

调用 ${line} 变量时使用双引号。

"${line}"

最新更新