【问题标题】:Why is zsh not able to read ~ (tilde) from a path in a script?为什么 zsh 无法从脚本中的路径读取 ~(波浪号)?
【发布时间】:2019-11-18 23:02:19
【问题描述】:

zsh 从脚本中导出PATH 时,它没有正确读取路径。

我的PATHexport PATH="~/path/to/stuff/",但是当我尝试运行位于该路径的命令时,zsh 找不到它。

当我将PATH 更改为export PATH="$HOME/path/to/stuff/" 时,zsh 能够运行该命令。

编辑:奇怪的是,我刚刚检查了这个,它再次与export PATH="~/path/to/stuff/" 一起工作。我的开发环境一定有什么奇怪的地方。

编辑 2: 我之前没有提到我正在阅读 export PATH="~/path/to/stuff/" 的脚本正在为主要使用 bash 作为 shell 的开发人员团队构建本地开发环境。我更喜欢使用 zsh,所以我必须让我的 shell 与团队中占主导地位的 bash 设置的所有配置配合得很好。

【问题讨论】:

  • 只有 unquoted 代字号会受到代字号扩展的影响。至少,使用export PATH=~"/path/to/stuff"
  • 正如切普纳所说。请改用export PATH=~/"path/to/stuff/"export PATH="$HOME/path/to/stuff/"

标签: shell path export zsh oh-my-zsh


【解决方案1】:

使用下面的代码得到你想要的:

export PATH=~/Desktop/Capture/
echo $PATH

# Result:    /Users/swift/Desktop/Capture/

虽然,当你使用字符串时,你会得到这个:

export PATH="~/Desktop/Capture/"
echo $PATH                      

# Result:    ~/Desktop/Capture/

所以要做到正确,您必须尝试这种方法

tilde=~
export PATH="${tilde}/Desktop/Capture/"
echo $PATH                             

# Result:    /Users/swift/Desktop/Capture/

附言此外,还有一个有用的命令可以扩展波浪号

这是一个例子

echo tilda=~

# Result:    tilda=~

zsh中使用magicequalsubst命令

set -o magicequalsubst
echo tilda=~    

# Result:    tilda=/Users/swift

【讨论】:

  • 这很有趣,我没有意识到这一点。这对zshbash 是否同样有效?我用 Edit 2: 编辑了我的答案,这解释了为什么它必须与 bashzsh 一起使用。
  • 它对bashzsh 都适用。但是magicequalsubst 命令是针对 zsh 的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-06
  • 1970-01-01
  • 2011-03-05
  • 1970-01-01
  • 2010-12-13
  • 2011-08-21
  • 2017-03-29
相关资源
最近更新 更多