Bash is an automation powerhouse for UNIX based systems, let’s learn it together.

Bash is an acronym of “Bourne-Again Shell”. It is a default command-line interpreter for UNIX and Linux based operating systems. In UNIX and Linux based operating systems, a terminal window is consist of a shell and Bash.

Bash is the powerhouse of these systems, which includes Linux distributions as well as MacOS among others. Learning bash can be super useful to perform automation. I’m someone who is always installing a new OS, and setting all my configs and programs manually each time can be super painful, but thanks to a few bash commands that I created I can fully automate the procedure.

Using bash I can install software, download configs from the net, set up configurations for my desktop, and much more.

Bash is super powerful and has lots of features. Today I’m going to present a few commands and statements that will help you automate a good deal of actions.


Bash shell scripting guide

For the bash shell scripting, we need to use the shell and the text editor. The extension of the shell scripting file is .sh. To create a new scripting file, create a text file, and save it with .sh file extension. I am using Ubuntu 20.04 for the demonstration and creation of Bash scripting files. The gedit is the default text editor for Ubuntu and many Linux based operating systems.

Let’s create a new file myscript.sh.

Create myscript.sh file

To execute the bash scripting file, first we need to change the permissions of the file and make it executable. The permission are changed by chmod +x command. In order to make the bash scripting file executable, you have to open up your terminal and navigate to the folder or directory where the file is stored, and execute the following command.

chmod +x filename

Now let’s execute:

chmod +x myscript.sh command.

Give the file execution rights

Execution of bash script file:

The bash script file can be executed in two ways:

  1. bash filename
  2. ./filename

To execute the bash file, navigate to the folder or directory where the bash file is saved. In the first way of execution simply write bash and then the filename on the terminal and hit enter. The bash script file will be executed.

In the second way, write “./filename” on the terminal and press enter. We will use the second way for executing the bash file throughout this tutorial.

In this basic guide of bash scripting, we will cover the following things:

  1. Echo command
  2. Comments in bash scripting file
  3. ls command
  4. Variables declaration and usage
  5. Conditional statement
  6. Loops in Bash

#bash #linux #ubuntu #programming #developer

Introduction to Bash for Beginners
4.70 GEEK