×


Create and Run a Shell Script in Linux Mint 20 - Step by Step process to perform this ?

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.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform Linux Mint related queries.

In this context, we shall look into 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 .


How to create a Shell Script in Linux Mint 20 ?

To create 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. 

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 such as:

#!/bin/bash
echo "Hello World! I have just written my first shell script"

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. 

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.


How to run 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 Script.sh

Executing this command will display the message that we have written in our shell script.


Alternatively, you can also execute the following command to run the very same shell script:

$ sh Script.sh

You can easily see that the output of this command and the one that we executed above is exactly the same.


[Need urgent assistance to install Missing packages on Linux Mint? Contact us here. ]


Conclusion

This article covers the method of creating and running a shell script in Linux Mint 20.

With this, you can write and execute complex shell scripts in Linux Mint 20 very easily.


To write and execute a script:

1. Open the terminal. Go to the directory where you want to create your script.

2. Create a file with . sh extension.

3. Write the script in the file using an editor.

4. Make the script executable with command chmod +x <fileName>.

5. Run the script using ./<fileName>.


To save a .sh file in Ubuntu:

1. Run nano hello.sh.

2. nano should open up and present an empty file for you to work in.

3. Then press Ctrl-X on your keyboard to Exit nano.

4. nano will ask you if you want to save the modified file.

5. nano will then confirm if you want to save to the file named hello.sh.


To Make a Bash Script Executable in Linux:

1) Create a new text file with a . sh extension.

2) Add #!/bin/bash to the top of it. This is necessary for the “make it executable” part.

3) Add lines that you'd normally type at the command line.

4) At the command line, run chmod u+x YourScriptFileName.sh.

5) Run it whenever you need!