How To Create Bash Scripts To Automate Processes on Linux

When we use a bash script in Linux we find different types of scripts such as SH and BASH and the main difference lies in the platform on which the script can be executed, so the scripts with SH extension can be executed on any Shell, such as macOS or FreeBSD and not only in Bash, while scripts with a BASH extension can only be executed in BASH.


When we create a script, we can omit the use of extensions and instead implement shebangs with the aim that the interpreter identifies the use of the script and what is its final function.

When we enter a script, the shebang must always go first, because if it is not present, we run the risk that the script will not be executed.

The most popular shebang is

#! / Bin / bash.

Create Bash Script on Linux

The objective of the script is to execute a series of commands defined with the goal of automating complex tasks and thus saving time and resources.

To create our first script we will execute the following line:

nano myfirstbashscript

Inside the new file we will add the first line which is the following:


There we can start to add the lines that we consider necessary to be executed by the script, for example, if we want to update the system we will enter the following:

sudo apt update;sudo apt upgrade -y

We save the changes using the key combination Ctrl + O, and we left the editor using Ctrl + X.

Assign Permissions to the Script in Linux

Once the script has been created, we must assign the respective permissions to execute it, for this we will execute the following line:

sudo chmod +x myfirstbashscript

Run the Script on Linux

Once the script is created we have the following options:

  • To execute SH files
sudo sh
  • To execute BASH files
sudo bash script.bash

Alternatively we can run any script regardless of its extension we can run the following line:


Create Script as a Binary in Linux

A binary script is one that is executed just by typing its name in the terminal and to convert a script into binary we must use the chmod command to be executable in the following way:

sudo chmod + x

Once the file is executable, it must be moved to the user's path by executing one of the following options:

sudo mv /script_path /usr/bin/

sudo cp /script_path /usr/bin/

With this we can execute the script simply by entering its name in the terminal.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *