How to run Bash Shell Script?

To run bash script, you must follow the below steps-

  1. Write a code inside a file that follows Bash scripting syntax and semantics. Save that file and provide a suitable name to it.
  2. Assign execute permission to the above-created file using chmod command.
    #chmod u+x filename
  3. Run the script by following any one the following methods-
    #bash filename
    OR
    #./filename

Let's practice the above steps with the help of a simple example-

echo "Hello Bash Shell."
echo "This is my first Bash Script."

Suppose the filename is firstprogram.sh. We will assign execute permission to firstprogram.sh file.

#chmod u+x firstprogram.sh

Now, we will run this file

#bash firstprogram.sh
Hello Bash Shell.
This is my first Bash Script.
OR
#./firstprogram.sh
Hello Bash Shell.
This is my first Bash Script.