While working in Linux OS, there are some commands that you need to run quite frequently. Instead of typing and executing those lengthy and repetitive commands one by one in a shell, a shell script allows you to run all those commands automatically. All you require is to put all those commands in a file and then run that file. You can execute a shell script multiple times which helps to saves time in executing every single command one by one.
Typical uses of shell scripts are as follows:
- Monitoring various tasks like disk usage, uptime, etc
- Automating backups, user account creation, etc.
- Launch multiple programs with a single command
In this post, we will be showing you how to create and run a shell script in the CentOS system. We have used the bash shell that is the default shell in CentOS.
Steps to Create and Run a Shell Script
Here are the steps you need to follow to create and run a shell script in CentOS.
- Launch terminal and move to the directory where you need to create the shell script.
- Use any text editor like Nano, vi, etc to create a new file with a .sh extension.
- Now enter commands you want the script to run.
- Make the file executable with command chmod +x <script_name>.
- Run the script using either ./<script_name>, bash <script_name>, or sh <script_name>.
Now let’s explain the steps:
Step 1: Launch Terminal
Firstly, you will need to launch the Terminal in your CentOS system. To do so, hit the super key on the keyboard, and then from the left panel that appears, click the Terminal icon.
Step 2: Create the Shell Script
First, move to the directory where you need to create your script. You can do so with the cd command followed by the directory name as follows:
$ cd directory_name
We will create the script in the Home directory which is our current working directory.
Now, create a new blank file with .sh extension. You can do so with the use of any text editor. To create the script file using the Nano editor, use the below command:
$ sudo nano script_name.sh
For instance, the command to create the script file named “sample.sh” would be:
$ sudo nano sample.sh
This will open a new file named “sample.sh” in Nano editor.
Step 3: Enter commands in your script
Now in the new file you have created above, enter the commands in the same order you want your script to run. Also keep in mind the points below, when you write a shell script:
- Start writing the shell script with “#!/bin/bash”
- Enter each command in a new line
- To add a comment, start it with #
Now in the script file, add “#!/bin/bash” in the first line and then add commands (each in a separate line) you want to run by the script. Here we are going to create a simple script that will print a message on the shell using the “echo” command. For this purpose, we have added the following command in the script:
echo “This is a simple script to print a message”
You can add any commands of your choice. Once done, save and close the script file. To save the file, press Ctrl+O and then press Enter. Then to close the script, press Ctrl+X.
Step 4: Set Execute Permissions
Now you should make your script file executable, which can be done using the below command:
$ sudo chmod +x script_name.sh
For instance, to make “sample.sh” executable, the command would be:
$ sudo chmod +x sample.sh
Step 5: Run the Shell Script
Now your shell script is ready to run. There are three different commands to run a script which are described below. You can use either of these commands for running your shell script. Make sure to replace the script_name.sh with your script name.
$ bash script_name.sh
$ sh script_name.sh
That is all there is to it! In this post, we have discussed how to create and run a simple shell script in CentOS 8 system. By following the same procedure, you can easily create and run even complex scripts and automate repetitive tasks. If you are using other Linux distributions, you can visit our posts on how to create and run a shell script in Ubuntu, Debian, and Mint.