How to write a shell script in Ubuntu 20.04 LTS

s2 0

What is a shell script?

A shell script is a Linux-based script into which commands are written, and when a user runs the script, all of the commands in the script are executed in sequence. Think of it this way: you have a task that you need to do that requires the user to write a certain number of commands, so it is difficult to write those commands one by one and then execute them. This is where the shell script comes in.

To accomplish this task, just write all of these commands into a single script and save that script file somewhere. Then, when the user needs to perform a specific task, all they have to do is run the saved script and the task will run without having to rewrite each command one by one. The shell is an interpreter of the commands that the user writes.

How do I write a shell script?

We’re going to use one of the built-in editors in Ubuntu 20.04 LTS to write a simple script. The editor is called “Nano” and the shell scripts have the extension “.sh”. File extensions on Linux are optional, but it is recommended that shell scripts be named with the “.sh” extension.

Now you have to enter “nano” at the very beginning in the terminal window to open the text editor. This is usually already installed in Ubuntu.

The above command opens the nano editor, which looks something like this:

The script usually starts with #! / bin / bash So you need to write this first. So the list of commands is as follows:

echo "welcome"
echo "this is the whole list of dir"

When you have finished using the above commands, press CTRL + X to save the script and exit. After that, the system will ask you for confirmation and then ask you to enter a name for the file. I will save the file as “”. It will look like this:

Save changed buffer

Press “and” confirm.


After you’ve done this, the editor will exit and your script will be saved.advertising

So far you have successfully created a simple script, now the script has the default permission rw – -r- -r (the first flag is for the current user, the second is for user groups, and the third is for others). The authorization format consists of binary numbers that represent authorizations. The basic authorizations are 4 2 1.

  • Read and read authorization is 4. assigned
  • Write-write authorization is 2. assigned
  • Execute-Execute permission is assigned 1

To run the saved script, you need to change its permission to 7 7 4. The concept of this is that if you want to give a rwx (read, write, execute), you have to add 4 (read) + 2 (write) +1 (execute), which adds up to 7. So this script must have 774 permissions in order for the user to run it.

Make the shell script executable

You can check the permission of a specific file with the following command:

 ls - l

In order to change the authorization of the saved script, you have to write the following command in the terminal:

sudo chmod 774

Grant executable permissions to the script

After pressing Enter, you will be asked for the password of the current user, after entering the password, the permissions for the file will be changed.

To run the script, all you have to do is “./Name of the script “.


Run the script

As a result of running the script, you will get something like this. Now, if you have followed all the steps carefully and as mentioned above, you will have created a script and executed it successfully.

Easy access to bash scripts with aliases

To make it easier to automate the script, create an alias for the script you created.

First you need a file called. create .bash_aliases in your home folder. In the terminal you have to enter the following command:

touch .bash_aliases

Create a bash alias script

Once created, open the file with the command:

nano .bash_aliases

Edit bash alias file

After typing the above command, Nano will pop up and look like this.

Then you need to enter:

alias dir= ""

This saves an alias for the script you created and that alias saves the user from writing “./ ” every time you need to run the script.

After creating the alias, all you have to do is type “dir” into the terminal and the script will run.

That’s the end of the article, we’ve covered almost everything about shell scripting. If you follow all the steps carefully and as described in the instructions, you will have successfully created your first simple script. Now you know what a shell script is, you know how to create a script, you know the permissions system of files, and you know how to create aliases. So good luck creating other scripts to help you.

How to write a shell script in Ubuntu 20.04 LTS