How to execute a shell script in Linux

To execute a shell script in Linux, you need to follow these steps:

  1. Give execute permission to the script:

     
    $ chmod +x script.sh
  2. Run the script:

     
    $ ./script.sh

You can also run the script by specifying the interpreter, like this:

 
$ sh script.sh

Or:

 
$ bash script.sh

If the script is located in a directory that is included in the PATH environment variable, you can run the script by simply typing its name:

 
$ script.sh

Note: Make sure the first line of the script starts with #!/bin/bash or #!/bin/sh depending on which shell you want to use to interpret the script. This line is called a shebang and tells the system which interpreter to use to run the script.

Leave a Comment