Macos 路径名中的转义空格

Macos 路径名中的转义空格,macos,bash,sed,Macos,Bash,Sed,我试图转义路径名/Volumes/NO name中的空格。我需要通过bash脚本访问此目录中的文件。我使用了sed和printf命令,但这两个命令似乎都不起作用。到目前为止,我掌握的代码是: while read line do if [[ -d ${line} ]]; then echo "${line} is a directory. Skipping this."; elif [[ -f ${line} ]]; then spacedOutLine=`echo ${

我试图转义路径名
/Volumes/NO name
中的空格。我需要通过
bash
脚本访问此目录中的文件。我使用了
sed
printf
命令,但这两个命令似乎都不起作用。到目前为止,我掌握的代码是:

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}

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

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

"${line}"

不要试图手动转义变量值中的空格。只要在变量展开时引用它,就可以保留空格。
"${line}"