Unable to execute bash scripts even as root?
Solution 1
That can happen if you have mounted the file system with the "noexec" option. You should remove it.
Solution 2
Script needs be executable. Use this:
chmod +x <script-name>
Solution 3
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.
Solution 4
Use chmod +x ./test.sh
this should allow you to run it.
Solution 5
Also, check to see if the directory/filesystem containing the script is nfs-mounted. root won't run scripts from nfs-mounted locations.
Comments
-
Marcin almost 2 years
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?
-
Rocky Inde over 10 yearsAlso, 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 the 'exec' option to the end of the options. -
Brad Koch almost 10 yearsDecent idea, but the OP already noted that
chmod 755
didn't work. -
TommyAutoMagically over 8 yearsThe
user
option can cause this issue, as well. Removing it allowed me to execute the binary in question. -
Gar almost 8 yearsOp got a "permission denied" , not a path access problem
-
Lan... almost 8 yearsif 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. -
Axel Stone over 6 yearsAnother possible reason in Ubuntu can be the default file manager behavior. Go to
filemanager->edit->prefferences->behavior
and checkexecute on double click
-
Gillespie over 4 yearsCan you expound??
-
kapitan over 4 yearsthis does it for me
+1
-
tripleee over 3 yearsThen you probablyehad the
noexec
problem which is explained in several other answers here. -
tripleee over 3 yearsThis 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.