【问题标题】:export alias in ~.bash_profile for a path having a filename containing space在 ~.bash_profile 中为文件名包含空格的路径导出别名
【发布时间】:2018-04-20 15:27:48
【问题描述】:

我正在尝试在 Mac 中的 bash_profile 中添加别名。我试图创建别名的文件的实际路径是

amar@admin:~/Library/Application Support/com.bohemiancoding.sketch3/Plugins 

$pwd

/Users/amar/Library/Application Support/com.bohemiancoding.sketch3/Plugins

我已经尝试了 2 个选项

选项#1

export sketch="/Users/amar/Library/Application\ Support/com.bohemiancoding.sketch3/Plugins"

这有类似的错误

amar@admin:~$ echo $sketch
/Users/amar/Library/Application\ Support/com.bohemiancoding.sketch3/Plugins
amar@admin:~$ cd $sketch
-bash: cd: /Users/amar/Library/Application\: No such file or directory

选项#2

export sketch="/Users/amar/Library/Application Support/com.bohemiancoding.sketch3/Plugins"

这显示了类似的错误

amar@admin:~$ echo $sketch
/Users/amar/Library/Application Support/com.bohemiancoding.sketch3/Plugins
amar@admin:~$ cd $sketch
-bash: cd: /Users/amar/Library/Application: No such file or directory

此文件名附近有问题Application Support

【问题讨论】:

  • 有一个简洁的选项shopt -s cdable_vars,它允许将目录路径保存在变量中,例如export sketch='/path/with space/',然后您可以在文件系统中的任何位置使用cd sketch。见the manual 和this Q&A。

标签: bash


【解决方案1】:

你必须在展开变量时引用它:

sketch="/Users/amar/Library/Application Support/com.bohemiancoding.sketch3/Plugins"
cd "$sketch"

引用不是可选的,您无法避免。

【讨论】:

  • 是的,它有效,但为什么在这种情况下我不必指定“”。导出 flink="/usr/local/Cellar/apache-flink/1.3.2" 。这里 cd $flink 也可以
  • 该字符串不包含任何元字符,如空格/*/[]/?
  • @AmarjitSingh 当您使用不带双引号的变量时,shell 会将其拆分为“单词”(基于空格、制表符和换行符),然后尝试扩展任何通配符 (* , ?, []) 它会在匹配的文件名列表中找到。执行此操作时,它不会注意变量值中的转义或引号。有时,这是您想要的,但(根据我的经验)通常不是。所以变量包含(或可能包含)任何这些字符,你应该双引号它。
猜你喜欢
  • 2020-01-25
  • 1970-01-01
  • 2011-01-08
  • 2013-10-13
  • 2013-05-29
  • 2013-04-07
  • 1970-01-01
  • 2011-11-20
  • 1970-01-01
相关资源
最近更新 更多