在 shell 脚本中使用带有空格的变量传递到 ls 时出现问题

发布于 2025-01-12 17:07:01 字数 262 浏览 2 评论 0原文

我试图弄清楚如何在将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城泪 2025-01-19 17:07:01

扩展变量后不会处理转义序列,因此不应将反斜杠放入字符串中。仅当您按字面输入不带引号的字符串时才需要它。

DST_DIR="Hello World"
ls "${DST_DIR}"

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.

DST_DIR="Hello World"
ls "${DST_DIR}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文