bash 在 zsh 下模拟 sh 时出现“权限被拒绝”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24601806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:48:53  来源:igfitidea点击:

Got "permission denied" when emulating sh under zsh

macosbashzsh.bash-profile

提问by SexyNerd

I just switched to zshas my default Mac OS terminal shell. However, I found it won't automatically hit the ~/.profilefile. After researching on Google, it looks like it can be solved by adding the following command in ~/.zprofileto emulate whatever in ~/.profile:

我刚刚切换到zsh我的默认 Mac OS 终端外壳。但是,我发现它不会自动命中~/.profile文件。在谷歌上研究后,看起来可以通过在其中添加以下命令~/.zprofile来模拟任何内容来解决~/.profile

emulate sh -c '~/.profile'

However, I got the follwoing error when terminal starting up:

但是,终端启动时出现以下错误:

zsh:1: permission denied: /Users/XXX/.profile

zsh:1: 权限被拒绝:/Users/XXX/.profile

Any idea why this is happening?

知道为什么会这样吗?

回答by burce

Sounds like you should be using .zshrc

听起来你应该使用 .zshrc

Add this to ~/.zshrc:

将此添加到~/.zshrc

EXPORT JAVA_HOME="whatever"

EXPORT JAVA_HOME="whatever"

And type $ source ~/.zshrcin your terminal window, or start a new shell instance.

然后$ source ~/.zshrc在你的终端窗口中输入,或者启动一个新的 shell 实例。

Follow up: this articlelists the startup files loading order which clarifies the confusion.

跟进: 本文列出了启动文件加载顺序,澄清了混淆。

回答by mklement0

To accomplish your goal, you'd have to use:

为了实现您的目标,您必须使用:

emulate sh -c 'source ~/.profile'  # Note the `source`; alternatively, use `.`

Without the source, ~/.profilewould run in a subshell, which defeats your intent (exports wouldn't "stick"); you have to sourcethat other file.
(The specific error you saw stems from an attempt to execute ~/.profiledirectly, without its being marked as executable. Note that shell profiles normally needn't be executable, because their only purpose is to be (automatically) readby a shell. It's a moot point, however, given that sourcingfrom within a shell is needed.)

没有source,~/.profile将在子shell中运行,这会破坏您的意图(exports 不会“粘住”);你必须的其他文件
(您看到的特定错误源于尝试~/.profile直接执行,而没有将其标记为可执行。请注意,shell 配置文件通常不需要是可执行的,因为它们的唯一目的是由 shell (自动)读取。这是一个没有实际意义的然而,考虑到需要从外壳内部采购。)

As for what zshinitialization file to put the command in:

至于zsh把命令放在什么初始化文件中:

  • On OS X, if you've made zshyour defaultshell, then ~/.zprofileworks, as all shells you'll open in a terminal will be login(zsh) shells, which will read that file.
  • Generally, though, ~/.zshrcis the better choice, as that file gets sourced by anyinteractive zshshell, whether it's a login shell or not. It's also the file to use with the Oh My Zshframework.
  • 在OS X上,如果你做了zsh你的默认外壳,然后~/.zprofile作品,因为所有的炮弹,你会在终端打开会登录zsh)弹,这将读取该文件。
  • 不过,一般来说,这~/.zshrc是更好的选择,因为该文件由任何交互式zshshell 获取,无论它是否是登录 shell。它也是用于Oh My Zsh框架的文件