在 shell 脚本中使用带有空格的变量传递到 ls 时出现问题
我试图弄清楚如何在将 shell 脚本传递给 ls 时处理带有空格的目录名。下面是一个代码示例 - 假设当前目录中有一个名为 Hello world
的目录,第一个 ls
有效,第二个 ls: Hello\ World :没有这样的文件或目录
。
DST_DIR="Hello\ World"
ls Hello\ World
ls "${DST_DIR}"
帮助。 :(
I'm trying to figure out how to handle a directory name with a space inside a shell script while passing it to ls. Below is a code sample - assuming that there is a directory called Hello world
in the current directory, the first ls
works, the second complains ls: Hello\ World: No such file or directory
.
DST_DIR="Hello\ World"
ls Hello\ World
ls "${DST_DIR}"
Help. :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
扩展变量后不会处理转义序列,因此不应将反斜杠放入字符串中。仅当您按字面输入不带引号的字符串时才需要它。
Escape sequences aren't processed after expanding variables, so you shouldn't put the backslash in the string. It's only needed when you're typing an unquoted string literally.