122

I am running a command

./startup.sh nginx:start

and I am getting this error message

zsh: permission denied: ./startup.sh

why could this be happening?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Reacting
  • 5,543
  • 7
  • 35
  • 86

8 Answers8

237

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

Andrea Golin
  • 3,259
  • 1
  • 17
  • 23
  • 2
    If anyone has this problem with symlinks, I've just had the problem whereby a symlink had execution permission, but the original file did not. Might sound obvious, but make sure the original file has the permission too :facepalm: – Phil Gibbins Jan 07 '20 at 10:35
  • 1
    Is there any security risk of doing this? – ScottyBlades May 28 '20 at 18:52
  • 2
    @ScottyBlades you are raising a fair point, of course making a file executable could lead to a security issue, depending on what lies inside the file. Using chmod +x gives exec permission to user, group and all. If you want to grant the exec permission to a specific access class, you can pass it like a parameter before +x. As an example, to give exec permission just to the user, you can do chmod u+x ./script.sh – Andrea Golin May 29 '20 at 10:30
  • Awesome, thanks you. Adding u applies it just to the current user. Is there any way to apply that restriction after running the command without u? – ScottyBlades May 29 '20 at 12:24
  • u+x will give exec permission to file owner, not necessarily the current user. I don't know if I understand what you're asking, but yes, you can chmod a file whenever you want – Andrea Golin May 29 '20 at 12:46
  • btw, the change done by `chmod` will also be saved in git history, so after you commit the change, in your CI, the script will also have exec permission. – peng37 Apr 24 '23 at 12:40
38

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.

Gefilte Fish
  • 1,600
  • 1
  • 18
  • 17
7

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

Suraj Pawar
  • 87
  • 1
  • 2
3

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

0

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
Matthias
  • 1,130
  • 10
  • 8
0

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?

node_saini
  • 727
  • 1
  • 6
  • 22
  • I do not see how this answers the question at the top of this page, but it should. Please [edit] according to [answer] or delete the answer. Otherwise it risks being flagged as "not an answer" and being deleted. – Yunnosch Sep 26 '22 at 11:55
-1

add sudo before command start, will save your time like

sudo anyTemninalCommand
Jagveer Singh
  • 2,258
  • 19
  • 34
-1

PLease use "cd" before going to any directory in terminal

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34484638) – ChrisB Jun 05 '23 at 18:39