作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想将 /c/users/me/dir name
存储到一个变量中,以将其传递给 cd
系统调用。
打字时有效:
$ cd '/c/users/me/目录名称'
或
$ cd/c/users/me/dir\名称
但如果我存储它则不起作用:
$ dirname="'/c/users/me/dir name'"
$ cd $dirname
$ bash: cd: /c/users/me/dir: No such file or directory
同样的结果:
$ dirname=('/c/users/me/dir name')
或
$ dirname=(/c/users/me/dir\ name)
哪种存储方式才是正确的?
最佳答案
用空格双引号您的路径变量,以保留它,
dirName="/c/users/me/dir name"
cd "$dirName"
实际上,dirname
是一个内置的 shell,建议使用备用名称以避免与实际命令混淆。
从 man bash
页面,
Enclosing characters in double quotes (‘"’) preserves the literal value of all characters within the quotes, with the exception of ‘$’, ‘`’, ‘\’, and, when history expansion is enabled, ‘!’.
关于bash - 如何将带有空格的路径存储到 bash 中的变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41995023/
我是一名优秀的程序员,十分优秀!