Create shell scripts

A shell script begins with a line that identifies the shell that’s used to run it. If you use the bash shell, your shell script must begin with this line of text:

#!/bin/bash

The first two characters (#!) represent the special code that tells the Linux kernel that this is a script. The rest of the line represents a shell that will be used to run the program.

In order for a script to run, it has to be executable. This is done using the chmod command with the x option. For example, to make the script my_script executable for all classes of users on the system, we need to run the following command: chmod a+x my_script

linux making script executable

To run the script, just enter the absolute or relative path to where it’s stored. For example, to run our script, we could type the following commands:

linux executing a script

In the picture above, you can see that we have executed the script using the absolute path first. After that, we have executed the script using the relative path (./ represents the working directory).

One more thing you should know before writing your first script is that the the pound sign (#) sign represents a comment. The interpreter ignores everything after the comment.

Geek University 2022