Create and Run Shell Scripting in Linux

In this guide, you will learn to Create and Run Shell Scripting in Linux. Shell Scripting is an open-source program that is used to write commands for the shell and execute them. It is a good option to combine a series of commands in a single script that can be stored and executed anytime.

This guide tries to show you how to create a simple script, run it, and comment on the shell script with examples.

Learn To Create and Run Shell Scripting in Linux

Now you can follow the steps below to see how you can create and run bash shell-based shell scripting on Linux distributions.

Step 1 – Create a Shell Script File in Linux

You can create your script files with text editors like vi editor or nano editor. At this point, you can use your desired text editor to create and open a script file and put your scripts in it. Then, you need to make your file executable. To do this, you can follow the steps below.

First, use the following command to create your file: Here we named it script.sh and use vi to create it.

vi script.sh

This will open an empty new file for you.

Next, you can add your desired commands to it. Here as an example, we add the following command:

echo hello-world

When you are done, save and close the file.

Now you should make your file executable. To do this, run the command below:

chmod -R 777 script.sh

Step 2 – Run a Shell Script File in Linux

At this point, you can run your script file by using the following command:

./script.sh

In the output, we get the hello world message:

hello-world

Step 3 – Comment on the Shell Script in Linux

Commenting on the shell scripting is an important task. This will help you to understand the usage of the script, etc. For example, in the above file, we want to comment that it is a sample scripting. To do this, we can open the file and add the following comment on it:

#sample test scripting
echo hello-world

When you are done, save and close the file.

Then, run your script file, it will not show the comment in your output results.

Step 4 – Use #!/bin/bash in Shell Scripting

You may want to use the #!/bin/bash in the first of your shell script. It tells the OS to invoke the specified shell to execute the commands that follow in the script. In simple words, it uses Bash as a default shell to run the script.

For example, in our sample script file, we use it like the following:

vi script.sh
#!/bin/bash
echo hello-world

When you’re done, save and close the file. Then, make your file executable and run it.

Conclusion

At this point, you have learned to create a sample shell scripting file, run the file, and use comments in the script file in Linux. Also, you have learned what is the usage of the #!/bin/bash in Shell Scripting.

Hope you enjoy it. You may be interested in these articles:

Execute Shell Commands in Python with Examples

Delete Files and Directories in Linux Terminal

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Stay informed and not overwhelmed, subscribe now!