My Board
How to Run Bash Script - Printable Version

+- My Board (https://ellohost.com/forum)
+-- Forum: Tutoriel EN (https://ellohost.com/forum/forumdisplay.php?fid=8)
+--- Forum: Others (https://ellohost.com/forum/forumdisplay.php?fid=20)
+--- Thread: How to Run Bash Script (/showthread.php?tid=134)



How to Run Bash Script - aaron - 08-12-2023



   

How to Run Bash Script


   

Now that we have written a bash script, let’s learn how to run it from the terminal. There are three methods to
        do it – using the bash command, using the ./ command, and running the script from a different directory.


   

Using the Bash Command


   

The first method is by using the bash command from the appropriate directory. For example, you may have a
        function.sh bash script containing simple echo functions in the Test/Bash directory.
        You have to open the directory first by using this command:

   
cd Test/Bash
   

Then, execute the following bash command to run the bash script:

   
bash function.sh
   

You should see the output like this:

    Output
   

If you try to run the script without the bash command, you’ll receive a command not found error
        message.

    Error

   

Using the ./ Command


   

You can run a bash script without the bash command. However, you have to set the file to have the execute
        permission using the following command from the appropriate directory:

   
chmod +x function.sh
   

This command modifies the file permissions so that everyone can execute the file. Once you’ve done that, execute
        the bash script by using this command:

   
./function.sh
   

If you don’t set the permission correctly, the terminal will print a Permission denied error message:

    Permission Error
   

Like the bash command, you’ll also get a command not found error if you don’t use ./ in
        your command.


   

Run the Script From a Different Directory


   

Another alternative is to run the script from a different directory. After you create the bash script, use the
        pwd command to find your current directory. Once you know the path, you can run the script from any
        directory. For example, use the following command to run function.sh from the home directory:

   
bash Test/Bash/function.sh