How to execute a bash script?
Solution 1:
You have to make the file executable. You can do that with
chmod +x <filename>
where is the name of your script and then you have to prepend it with ./
to instruct the shell to run a file in the local directory, like:
./script.sh
You can only run files that are in your PATH
or that you specify a path to them. ./
, the local directory, is not in the PATH
by default because someone may use it for nefarious purposes. Imagine a script called ls
dropped in a directory, you go inside that directory, run ls
and that script does something bad.
While you are at it you may want to make it more portable by running shell instead of bash by using:
#!/bin/sh
or by running bash no matter where it is installed as long as it is installed:
#!/usr/bin/env bash
Solution 2:
In addition to the advice by Fernández, precede it with a point, like this:
./myscript.sh
For security reasons the current directory is never included in the execution path.