Bash scripts will not run without typing "bash" in front of it

Solution 1:

  1. Make sure you start the script with ./script or full path or whatever. Just script may not work (it works if the directory is in $PATH, like /usr/bin), since on UNIX systems it's not a habit to have the current directory in your path (for security reasons, and it's good!)

  2. Make sure the script is executable, for example: chmod +x script will made it executable.

  3. Make sure, you have #!/bin/bash as the first line in your script. Also make sure, that it's not edited with some kind of Windows editor, since those often uses the "DOS type" of eol (end of line) which differs from the UNIX one (if the checklist above is OK, but you got "bad interpreter: no such file or directory" or so, even if it's /bin/bash, this is often the reason, as the non-printable - so you usually don't see it - \r will be treated as the part of the path of the interpreter)

Others already mentioned: it's important to have /bin/bash if you use bash features, also /bin/sh was symlinked to /bin/bash, but now-a-days (as far as I noticed) it's symlinked to dash which won't provide bash compatibility, only the POSIX sh. It's quite important, even quite expensive softwares at our firm have this issue: scripts contain #!/bin/sh as the first line but it depends on bash functionalities as well.

Solution 2:

Make sure the first line of the file reads:

#!/bin/bash

If the shebang is #!/bin/sh, you should not use any bash-specific features, only POSIX features. Even if /bin/sh is a symlink to bash, bash will run in a POSIX compatibility mode when run as sh, disabling some (but not all) bash features.

You'll also need to make sure the script is executable, of course.