终端错误: zsh: 权限拒绝: ./startup.sh

我在执行命令

./startup.sh nginx:start

我收到了这个错误消息

zsh: permission denied: ./startup.sh

为什么会这样?

300087 次浏览

Be sure to give it the execution permission.

cd ~/the/script/folder


chmod +x ./startup.sh

This will give exec permission to user, group and other, so beware of possible security issues. To restrict permission to a single access class, you can use:

chmod u+x ./startup.sh

This will grant exec permission only to user

For reference

Alternatively you can use bash:

bash startup.sh

Then you don't need execution permission.

In MacOS Catalina, Apple has replaced bash with zsh as default shell. This can mean, that they intend to remove bash in the future, so this might not be an option later, but with Catalina it still works.

Starting with macOS Catalina, Your Mac uses zsh as the default login shell and interactive shell. You can make zsh the default in earlier versions of macOS as well.

How to change your default shell Whether your user account is configured to use zsh (recommended), bash, or another shell, you can change the default shell from Users & Groups preferences or the command line.

  1. From Users & Groups preferences
  2. Choose Apple menu  > System Preferences, then click Users & Groups.
  3. Click the lock , then enter your account name and password.
  4. Control-click your user name in the list of users on the left, then choose Advanced Options.
  5. Choose a shell from the ”Login shell” menu, then click OK to save the changes.

Follow link for more details - https://support.apple.com/en-in/HT208050

add sudo before command start, will save your time like

sudo anyTemninalCommand

Another annoying error can be n typo in the sh script.

In the following example, the ZSH error message does confusing. ZSH does tell you zsh: permission denied: startup.sh. But you have access rights to your script. The issue is the invalid Shebang line in the script:

#!/usr/local/bin sh

The right Shebang line can be e.g.:

#!/usr/bin/env sh

You need to grant execution permission to your file. Here's a way to do that.
Navigate to the folder that contains your file and run this command-

chmod 755 <filename>

The three digits of the number 755 represent permissions for the three types of users- Owner, Group, and Others.

So, 755 represents-

Digit (octal) Binary read write executable for
7 111 1 1 1 owner
5 101 1 0 1 group
5 101 1 0 1 others

Thus this command gives all three permissions- read, write and executable to the owner, while only read and executable to group and others.

More details about permissions in MacOS/Linux are discussed here- https://askubuntu.com/questions/932713/what-is-the-difference-between-chmod-x-and-chmod-755

Below worked for me but I don't know why.

My file permissions before making the below change were -rwxr-xr-x. Even though I had the execute permission but still i got the permission denied error.

I am using vs code editor. I executed chmod +x filename and the file permissions still remained the same. The only difference this time was that I was able to run the file. Something changed about the file but it's not visible. The reason why I say it's not visible is that in the source control tab of my editor, my new file and old file looked 100% the same. If I stash my changes and execute the file then again same error.

I don't know why and how it worked but it's worth a try.

I will be more than happy if someone can explain the reason to me why it did not work earlier as I had the same permissions? Also, what changed in my file which is not visible to me?