Bash Scripting — For Beginners and Intermediates

Kavita Verma
SiteSonic
Published in
5 min readJun 26, 2019

Bash scripting is one of the few aspects of development that is used by most and talked by very few.

It is the most critical and yet the very less known aspect of computer programming, especially among the new programmers. In fact, it the base of any field that deals with heavy use of Linux and its subsystems.

In this blog, we will talk about this rarely known aspect of programming. The following text will tell you about the functionality of Bash, the uses of a Bash script and the various Bash script commands at beginner/intermediate level.

What is Bash?

Bash, also known as the Bourn-Again-Shell is an advanced and widely accepted command language interpreter for the GNU systems. The interpreter is responsible for interacting with the GNU operating system and making it more communication-friendly for the operator.

In other words, Bash is the component of the computer which enables the user to control the innermost component s of an operating system and command them for a specific task, using plain texts.

What Is Bash Script?

Bash script refers to the command or set of stored instruction which regulate the reaction of the bash interpreter to a command or instruction. The command file is stored as a plain text file and can contain a series of commands or function for the interpreter. You can use it to run, single multiple or a long list of commands without any problem. In fact, the script is also usable for running loops and conditional construction.

The developer prefers this script to run administrator tasks such as system log evaluation, disk backup, and more. They are also useful for simplifying the complex program installation processes. It’s because these scripts can run more than one external program with a few lines of command and have decent decision-making abilities.

What Beginners Need To Know?

Before you enter the world of Bash scripting and start writing them, there are things that you must know about it. Have a look:

You can write and execute Bash on Windows operating system through Cygwin

· Bash script works with a universal language, so the type of operating system or the machine does not matter

· It is possible to run a script written on windows operating system on Mac machine and vice versa without any alterations

· The Bash script can access the device at an administrative or superuser level, so test your script before putting them to use

Now, that you are familiar with the typical bash scripting facts, let’s begin with your first script.

I. Creating a new file

The first step of the scripting process is to create a new file with extension “.sh.” You can create it using an editor like VI editor. You can also start the process using a standards text file and then save it as a .sh file once you finish the code.

II. Writing the Instructions

The next phase of the Bash scripting is to write the code. Start it by writing the command “#!/bin/bash” at the top of your file. The ‘#!’ is known as the shebang and is responsible for directing your script to the interpreter. The other part, “bin/bash” is to indicate the type of shell that you are using.

After this command, you can begin with the coding. Most of the variable, commands, and rules will remain the same as a standard program. However, there are some exceptions like in this case you will use the square bracket.

Moreover, Bash operators are different from most other programming or scripting languages. Like it replaces == with –eq,!= with –ne and < by –lt.

Another thing that you have to consider while writing the program is that the variable will be declared normally, but you have to add a $ sign while invoking it. You will also have to add “echo” command before the text that you want to display in the result.

III. Testing the Code

Once you have completed the code, the final step will be to test its functionality. To run the file firstly open the terminal and provide it executive permissions, using the “chmod+X <filename>” command. Then type in “./<filename>” to run the script.

In case, you have moved the file from its originals destination, replace the <filename> with the new oath rather than the filename.

Some Intermediate Level Commands

At the beginner level, you can stick with the standard programming commands, but as you grow in the field, you will come across a lot of other useful commands. These commands will help you to create more valuable and executable programs.

Some of the programming features that you will come across are:

Quoting

Quotes help prevent Bash from interrupting with the special characters in an argument. Like it can prevent Bash from splitting the argument that contains a space. You can quote the text using inverted commas. The text inside single inverted commas will appear as it is while the text between double commas will displace the text along with variable expansions.

Text Filtrations

Sometimes you have to find the line and files which contains a specific word or set of words in the Bash can help you in it. It has a power tool known as ‘grep’ that can filter out the required text from the given directory.

Moreover, you also have the commands like ‘sed and awk’ which can help you to manipulate or edit the text of your files.

File Searches

The file command of base programs is an intermediate level command that can help you to search, delete, or run a file using the command window. If you have more than one file with similar names, Bash will also allow you to apply filters for a more precise search result.

Multi-tasking

Usually, the programs that you open on Bash run in series. You have to wait for the first program to close before the next one could get executed.

Now, you cannot run them all together, but you can pause the running program and execute another program by typing ^c.

The paused program will stay in the background, and you can resume it by using the ‘bg’ command.

The Final words

Bash scripting is a doorway to an anonymous amount of opportunities in the computer development section. It not just a simple tool, its a set of tools and option for dealing with the whole computer system. Learning to use it allows you to perform everything from a simple file search to a complex task like editing a file or converting images without using a mouse. Its text-based interact will remove all the barrier and limitation of the graphical user interface.

Even the experts like Akram Artoul, CEO of SiteSonic believes that Bash scripting is an essential skill for every website developer and coder.

Their team can help you not only with the Bash script but also with many other web developing aspects like frame works, drop wizards and more.
Contact SiteSonic for more details and service related to website development for mobile apps.

--

--

Kavita Verma
SiteSonic

Kavita's world revolves around highly technical sphere! She loves to keep a tab on what's happening in the world of technology.