Introduction:
A shell script is an alternative method of carrying out those operations automatically that have to be carried out quite frequently. Therefore, carrying them out manually is a hassle. Moreover, at times you have such complex scenarios for which you do not want to do the manual working every time such a scenario is encountered. Therefore, today we are going to tell you how you can create and run a shell script in Linux Mint 20. However, if you want to do the very same thing on any other Linux distribution, then you can take a look at our article on creating and running a shell script in Debian 10.
Method of Creating a Shell Script in Linux Mint 20:
For creating a shell script in Linux Mint 20, we will first create a Bash file by running the following command:
sudo nano Script.sh
Here, you can replace Script with any name of your choice that you want to keep for your shell script. We have named our shell script as MyFirstShellScript.sh as shown in the image below:
When an empty file will open with the nano editor, we will write a sample Bash script in it. The first line of this script indicates that it is a Bash script (you can also write “sh” instead of “bash” over here). Then we have written the code to print a sample message on the terminal while using the “echo” command. You can display any random message of your choice.
After saving your file, you can even confirm its creation by running the following command:
ls
This command will list down the contents of your current directory in which you have created your shell script. The name of the newly created shell script is highlighted in the image shown below:
Now, we can also check the contents of this script by running the following command:
cat Script.sh
Here, you have to replace Script with the name of your shell script whose contents you want to view.
The contents of our first shell script are shown in the image below:
Method of Running a Shell Script in Linux Mint 20:
After creating a shell script and confirming its creation, we can look into the method of running it. We can run a shell script in Linux Mint 20 by executing the following command:
bash MyFirstShellScript.sh
Executing this command will display the message that we have written in our shell script as shown in the image below:
Alternatively, you can also execute the following command to run the very same shell script:
sh MyFirstShellScript.sh
The result of this command is shown in the image below. You can easily see that the output of this command and the one that we executed above is exactly the same.
Conclusion:
This tutorial presented to you the method of creating and running a shell script in Linux Mint 20 in the most simplified manner. So now, by following the very same procedure, you can even write and execute complex shell scripts in Linux Mint 20 very easily.