Unable to execute bash scripts even as root?

41,525

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.

Share:
41,525
Marcin
Author by

Marcin

Developer

Updated on July 05, 2022

Comments

  • Marcin
    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
    Rocky Inde over 10 years
    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 the 'exec' option to the end of the options.
  • Brad Koch
    Brad Koch almost 10 years
    Decent idea, but the OP already noted that chmod 755 didn't work.
  • TommyAutoMagically
    TommyAutoMagically over 8 years
    The user option can cause this issue, as well. Removing it allowed me to execute the binary in question.
  • Gar
    Gar almost 8 years
    Op got a "permission denied" , not a path access problem
  • Lan...
    Lan... almost 8 years
    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.
  • Axel Stone
    Axel Stone over 6 years
    Another possible reason in Ubuntu can be the default file manager behavior. Go to filemanager->edit->prefferences->behavior and check execute on double click
  • Gillespie
    Gillespie over 4 years
    Can you expound??
  • kapitan
    kapitan over 4 years
    this does it for me +1
  • tripleee
    tripleee over 3 years
    Then you probablyehad the noexec problem which is explained in several other answers here.
  • tripleee
    tripleee over 3 years
    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.