In this article, we would cover how to execute shell scripts in Ubuntu. Shell scripts are nothing but a group of commands, functions etc. which we can otherwise execute directly from a shell. Sometimes, we just have to run a particular set of commands regularly. Its unproductive if do it all by ourselves. Therefore, we just bunch those commands together in a file and automate it.
Here, we will discuss two methods to execute shell scripts –
- Passing script as an argument to the shell,
- Making the Script executable.
Execute shell scripts in Ubuntu
Method I. Passing script as an argument to the shell.
Let’s say we have a script – exscript.sh
To execute exscript.sh, we can issue the following in terminal –
bash exscript.sh
Here, we are making Bash to execute the script. If you have other shell installed, for instance Zsh (or Z shell) then we may use that as well,
zsh exscript.sh
Method II. Making the script executable.
Continuing with the above example – exscript.sh. We need to make our script executable first. This can be done through chmod command-line utility. Furthermore, if you want to know more about chmod. So, open a terminal and issue the following –
chmod u+x exscript.sh
Use ls -l to analyze the permissions –
ls -l
If you haven’t made changes to permissions earlier then, after making changes it would look something like –
-rwxrw-r--
See, the user has been granted execute permissions. Alternatively, we can also set permissions through graphical interface. Just right-click the script file you want to grant execute permissions. And, under Permissions tab, grant the necessary permissions. We always prefer to use a terminal.
Lastly, to execute the script –
./exscript.sh
In conclusion, we have covered how to execute shell scripts using two methods – first was passing script as an argument to the shell and other one by making the script executable using chmod command-line utility.