21

I have a weird problem, I cant execute bash script even as basic as:

#!/bin/bash
echo "me"

I am saving it as a test.sh and then do chmod 755 test.sh and once run ./test.sh getting:

bash: ./test.sh: Permission denied

Any ideas what could be causing this?

tripleee
  • 175,061
  • 34
  • 275
  • 318
Marcin
  • 5,469
  • 15
  • 55
  • 69

9 Answers9

26

That can happen if you have mounted the file system with the "noexec" option. You should remove it.

apaderno
  • 28,547
  • 16
  • 75
  • 90
themel
  • 8,825
  • 2
  • 32
  • 31
  • 3
    Also, to know quickly if your filesystem has been mounted with the 'noexec' option, use: `mount` And to remove the 'noexec' option, simply delete it from the list of options against the filesystem in the following file: `/etc/fstab`. Or alternatively [add](https://bbs.archlinux.org/viewtopic.php?pid=989973#p989973) the 'exec' option to the end of the options. – Rocky Inde Dec 28 '13 at 03:07
  • 2
    The `user` option can cause this issue, as well. Removing it allowed me to execute the binary in question. – rinogo Nov 05 '15 at 21:34
  • Another possible reason in Ubuntu can be the default file manager behavior. Go to `filemanager->edit->prefferences->behavior` and check `execute on double click` – Axel Stone Oct 31 '17 at 19:07
21

Script needs be executable. Use this:

chmod +x <script-name>
Abhinav
  • 329
  • 3
  • 8
  • 1
    this does it for me `+1` – kapitan Dec 02 '19 at 01:14
  • 1
    This duplicates another answer from several years back. It's probably a common beginner problem, but this particular instance of this answer shouldn't deserve more upvotes than the original, especially since the question already mentions the original OP had already made sure the permissions were correct. – tripleee Dec 23 '20 at 21:28
2

Although not directly pertinent to this particular thread; if a file has come form a Windows system there may be a CR/LF at the end of the line. This would affect all lines in the file, including the initial execution line, and would not be visible if you are viewing the file.

$ ./test.sh 
-bash: ./test.sh: /bin/bash^M: bad interpreter: No such file or directory

To see this, you could cat -A the file: $ cat -A ./test.sh #!/bin/bash^M$ echo "me"^M$

To remove, use dos2unix.

1

Use chmod +x ./test.sh this should allow you to run it.

Serem
  • 21
  • 1
1

Also, check to see if the directory/filesystem containing the script is nfs-mounted. root won't run scripts from nfs-mounted locations.

jceifrig
  • 11
  • 2
1

In macOS this can occur if a com.apple.quarantine flag exists. If you see a @ suffix on the permissions after running a ls -l on the script's path, execute ls -l@ *script_path* to confirm. Then run a xattred -d com.apple.quarantine *script_path* to remove the quarantine flag.

1

Try

ls -la

to see the actual rights and ownership of the file. To see if the chmod command actually worked. You might want to change the ownership along with the mod of the file check : http://www.tuxfiles.org/linuxhelp/fileowner.html

Stainedart
  • 1,929
  • 4
  • 32
  • 53
0

you need use ./test.sh when you in the directory of that file,if you don't,try PATH TO THE SCRIPT.or you can copy it to some directory of /data and chmod it for shell,then do the above steeps.if you still fail,it's ok because i have a same problem,i just did it success for once time.

Lan...
  • 105
  • 1
  • 6
  • Op got a "permission denied" , not a path access problem – Gar Jul 20 '16 at 16:06
  • if you are root user and still have that problem,so your shell is broken.i know that because i couldn't execute many commands of the sh shell(similar to bash) even i tried as root and it said permission denied like your,i couldn't change the permission.then i tried to copy them to my directory in `/data`,change permission and i could use commands again.but when i try to execute the script,it's no such file or directory. – Lan... Jul 20 '16 at 16:20
  • Then you probablyehad the `noexec` problem which is explained in several other answers here. – tripleee Dec 23 '20 at 21:17
-1

For filesystems which are mounted with the noexec by default, for example NFS, explicitly adding exec at the end helps, even when options provided earlier in the list default imply noexec as well, e.g. the user option.

So if you have one of those options:

  • noexec
  • user

Change them to:

  • exec or
  • user,exec

It is important to place exec at the end. Just removing noexec may help in certain cases, but not in all, if you are using other options like user before.

sebix
  • 2,943
  • 2
  • 28
  • 43