Loading...

Bash Scripting Tutorial – A complete beginner guide (2022)

Bash Scripting Tutorial – A complete beginner guide (2022)

Nowadays everything seems automated while developing something we often do many things that can be automated with bash scripting and it is a really powerful tool to learn it can make your development life cycle very easy and helps you to organize the workflow better.

In this blog, we will cover the basics of shell scripting and a complete guide on how to get started with bash scripting in 2022. This is going to be from scratch and fully beginner friendly, so don’t worry if you are not familiar with shell or bash scripting before. I will be going through each topic and explain to you with examples too.

Scripting Key Concepts

Before we go further, let’s just understand the three key concepts and what is the difference between them. Understanding this concept is really helpful when you are getting started learning Bash Scripting.

  • Terminal
  • Shell
  • Bash

Terminal

A terminal is a program that is used to interact with the shell to understand it in a better way, it is the screen of your command prompt or git bash terminal. Terminal or command shells are present in all operating systems like macOS, and Linux. etc.

Git bash

You can install the git bash terminal too which is quite a good and recommended one. To install the git bash click on this link

Shell

On the other hand, a shell is the command line where you can type in commands into a terminal window and those commands are interpreted somewhere.

For example, a simple command will help you to make stuff automate like a listing of all the files in a particular directory or change the current directory to a different one.

Like in this image, I am listing all the files which are present in the current directory on a particular folder or a disk with a command ls

shell

Bash

It is a Unix shell and command language, it was written by Brian Fox as a free alternative to the bourne shell, in order to understand it better and in more detail basically bash is just a command language and it is an alternative to the default to the shell

Bash is a command language interpreter for interacting with a computer from the command line. Bash works as a shell too because it helps in hiding the intricate details of the operating system and only shows the important things like data files, and other operations.

It is the most used Unix shell out there and it provides many features that make it useful and help the developer’s life cycle efficient.

What is Bash Script?

It is a series of scripting commands created and written in a file. These can be read and executed by the bash program without typing so many commands because of bash all the predefined commands are written and one should follow those commands to get the desired output from the bash command.

It executed the program one by one which means it gets completed in chronological order, and bash scripts are case sensitive too. So the only way to get the command executed is to type as written or given by the script file.

The bash script file is identified with .sh and if you are following the naming convention then the naming convention that should follow to run the script file is the same or you can simply run it without any file extension it works perfectly fine.

Difference between bash scripting and programming language

Bash scripting is technically a programming language but it is a lot different from the traditional language that is present out there. So to understand better, it is like a simple automation tool that helps to get rid of manual work that can be automated with the help of the bash scripting tool respectively to traditional programming which is a more general-purpose language that helps to design and solve the problem of real-time and than can variety of tasks.

Programming languages are mainly divided into three types i.e high-level language, machine-level language, and last is assembly-level language. Learning bash scripting language is quite easy because in this process you don’t need to have knowledge of development.

There are so many differences between programming language and bash scripting language some of the differences are listed below.

Bash Scripting Language Programming Language
It is a language that gets to bring any codes to the runtime environment.
It is a programming language
that is used to talk or communicate with electronic devices like computers, laptops,s, etc.
It is based on interpreted language and does not support compile time or compiler-based language.It is based on the compiler approach language. The compiler converts the code into machine language.
It is mainly used to create dynamic web applications and these applications require a host to convey.Programming language is mainly used for creating programs or applications which is based on the product of human needs and are self-executable.
Bash scripting is quite less complicated in writing or executing as compared to programming language.Programming language requires a lot of code to make one executable program and compare to bash scripting it is difficult as it requires numerous tasks.
Popular examples of bash scripting are Perl, Bash, Ruby, and Python. Popular examples of programming languages are Java, C, C++, Scala, and PHP.
difference between

Need of Bash Scripting

If you are thinking of becoming a hacker, cloud specialist, or anything in the IT world it is very much important to learn bash scripting. It has more interactive features than any other command scripting language. Bash handles different programs easily and helps in communicating between them.

It is good for day-to-day administration and it is better than any standard programming language when it comes to web app deployment, system administration, automated backups, and data crunching. And bash is better than when you want to run a lot of programs in series, which let’s python isn’t good at it. It can run a series of python, c++, or visual basics programs easily.

Basic commands you should know

These are the basics commands you should learn and get familiar with like:

pwd: It means Print working directory which gives you the detail of the current folder location. An example of a print working directory is supposed you want to know the path of a particular folder in this scenario we use pwd and it gives us the result with a proper location.

// It prints the physical directory with any symbol or special character present in it. pwd -P
Code language: Bash (bash)
pwd example

cd: This command is used for changing the directory of a file or a path that you are currently present. To make it simple suppose you are in a series folder in this folder there are many subfolders that contain a lot of series and shows.

And you click one other subfolder that contains “How I Met Your Mother” tv show but now you want to go to the “Friends” folder so you just click back. Like this, the cd command also helps you to change the folder or current directory.

// this will redirect you to the mentioned directory cd [directory-name]
Code language: Bash (bash)
cd example

ls: It means Listing the current folder files and all the applications present in them. An example of ls the command is it lists all the files to understand it better suppose you want to know the folders and files on a particular disk, you just need to type ls and it will give you the list of files and folders present on that disk.

// It prints the recently edited files first in order ls -1
Code language: Bash (bash)
ls example

mdkir: It helps you to create a folder or files of a particular location in the disk. An example for mkdir is simple just like when you create a folder in windows you can do this with commands just write mkdir the name of the folder.

// it display a message for every directory created mkdir -v
Code language: Bash (bash)
mkdir example

mv: It moves the file from one location to another one. An example of mv is simply moving a file from one place to another, just typing the file name and where to move in it will do the task.

// it gives you a ineraction and provide warning // before making any changes mv -i file_name second_file_name
Code language: Bash (bash)
mv example

Like this, there are many commands that you can learn and understand better. But the question arises where to learn?

Where to Learn?

There are many resources available to learn and get started in a bash scripting language, though there is not necessary for you should be an expert in this particular field even a beginner can start learning bash.

As a developer, you can only do so much with GUI. If you know how to work with the command line properly, it becomes a very powerful skill set. One of the course that codedamn provide is quite complete in its manner and this should be enough for you to learn bash scripting.

This course will teach you the basics of Linux CLI and how you should work with it. It will go over common commands, a text editor, and a few tips along the way, check this out!

Conclusion

Learning bash in the present time will surely increase your chances of becoming an awesome developer, because of the things bash offers. It will take you to the next level giving you the power to automate things like hacking networks but faster, creating virtual machines in the cloud but just faster.

Essentially this will make you a lot better at working with computers and if you can learn to wield this power, it will make you powerful and super valuable to the tech industries. So just learn it!

Sharing is caring

Did you like what Anas Khan wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far

Curious about this topic? Continue your journey with these coding courses: