What is a Shell Script?
A shell script is a Linux-based script in which commands are written. When a user executes the script, all the commands that are in the script are executed one after another. Think of it like this: You have a task for which you need to write a certain number of commands, and it is difficult to write and execute them one by one.
To complete this task, you simply write all the commands in a single script and save that script file somewhere. Then, when the user needs to do a particular task, he has to run the saved script, and the task is done without having to write all the commands again one by one. The shell is an interpreter of the commands that the user writes.
How to write a Shell Script?
We will use one of the built-in editors in Ubuntu to write a simple script. This will work with any current version of Ubuntu, including Ubuntu 20.04 and Ubuntu 22.04. As Ubuntu is a Debian derivate, you may use Debian Linux instead. The editor is called “Nano” and the shell scripts have a “.sh” extension. File extensions are optional on Linux, but it is good practice to name shell scripts with the “.sh” extension.
Right at the beginning, you need to type “nano” in the terminal window to open the text editor.
This is usually already installed in Ubuntu.
The above command will open the Nano editor, that will look something like this:
The script usually starts with #!/bin/bash, so you first need to write this. So, the list of commands goes as follows:
#!/bin/bash echo "Welcome to Vitux" ls echo "this is the whole list of dir"
The echo command is just used to show an informative text. When you are done with the commands that are mentioned above then you press CTRL + X to save the script and exit. After that, the system will ask you for confirmation, and then it will ask you to write a name for the file. I will be saving the file as “directories.sh”. It will look like this:
Press “y” to confirm.
After you do this the editor will exit and save your script.
Till here you will have successfully created a simple script, now the script has default permission of rw – -r- -r (the first flag is for the current user, the second one is for user groups and the third one is for others). The permission format consists of binary numbers that represent permissions. The basic permissions are 4 2 1.
- Read- read permission is assigned to 4
- Write- write permission is assigned to 2
- Execute- execute permission is assigned to 1
So, to execute the script you saved, you need to change its permission to 7 7 4. the concept of this is that if you want to give an rwx (read, write, execute), you will have to add 4(read)+2(write)+1(execute) that will sum up to 7. so the permission of that script needs to be 774 for the user to execute it.
Make the Shell script executable
You can check the permission of a specific file by the following command:
ls - l directories.sh
Now, to change the permission of the script you saved, you will have to write the following command in the terminal:
sudo chmod 774 directories.sh
After you press enter, it will ask you for the current user’s password. After entering the password it will change the permissions for the file.
Now, to run the script you just have to type “./nameofscript”.
You will get something like this as a result of executing the script. Now, if you followed all the steps carefully and as mentioned above, then you will have created a script and executed it successfully.
Easy Bash script access using Aliases
To make it easier to automate the script, you create an alias for the script you made.
First, you need to make a file called .bash_aliases in your home folder. You will have to enter the following command in the terminal:
Once it is created, open the file by using the command:
After you enter the above command, Nano will open and look like this.
Then you will have to type:
alias dir= ".directories.sh"
This way, an alias is stored for the script you created. This alias saves the user from having to type “./script.sh” every time you want to run the script.
So after you create the alias, all you have to do is type “dir” in the terminal, and the script will run.
So, that’s the end of the article. We have covered almost everything about shell scripts. 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. Good luck creating more scripts that will help you.