Introduction To BASH Scripting - Learn BASH | Part 1
Welcome to the introductory series on BASH scripting. This series will cover a complete guide on BASH scripting starting from the foundation of computer programming then progressing on to the basic constructs of this scripting language and finally, you will also create a simple mini-project using this scripting language.
BASH is short for Bourne-again Shell, a superset (as in updated version) of Shell scripting language developed by Brian Fox in the mid-1980s. Ever since it has been widely adopted as the default shell on most Linux distributions.
BASH Scripts Difficulty & Requirements
BASH is not difficult to learn but if you’ve had some exposure to any of the computer programming languages (like C, C++, Java, etc) then you’ll find it easier to grasp on quickly. However, fret not if you’ve not had any experience in computer programming. I’m pleased to let you know that BASH is easy to learn and challenging at the same time. And this series is intended for audiences like you, a newbie to computer programming.
For the requirements, you need a Linux environment like Ubuntu, Linux Mint, or Zorin OS, etc. And your patience, eagerness to learn and explore further.
Why learn BASH scripting?
So you might be wondering why learn BASH scripting? Luckily, there are many reasons and advantages for learning this scripting language but I will highlight some of my opinions:
- It’s a go-to for automating most of the work for developers and admins by writing bash scripts to perform tasks
- You will enhance your terminal skills
- It’s simply fun learning something new
So what’s programming?
Alright, let’s start with the basic question a newbie would have on her mind learning computer programming. So what’s programming? Programming is a list of step by step instructions telling the computer’s CPU what to do. Let’s consider our scenario, you are reading this article on a web browser. When you click a link or scroll the page contents, your computer is being programmed to listen and respond to your inputs accordingly.
That’s the ultimate goal (interacting with user inputs) of this BASH scripting series. But unfortunately, we won’t be creating big programs like Firefox or another word processor program because they are outside the scope of this series. Instead, we will learn some basic concepts of this scripting language, automation and of course finally creating a mini-project or a bash script in the final part of this series; a basic calculator program.
Overall, programming is like talking to a computer in the language it knows. Just as we humans have diverse languages, there are many different programming languages to communicate with the computer too. And BASH is one of them.
What tools do I need?
BASH is already the default shell on most Linux distributions so you don’t need to install anything. What you need are a text editor and a terminal program. I recommend using a text editor that supports syntax highlighting and is programmer friendly like jEdit, notepadqq, etc. And if you know your way around vi and emacs text editors, you can use them too.
8 Best IDE or code editors for Linux
Prepping up
Open your terminal program and create a directory called bash by keying in the below command –
mkdir bash
From now on, we will use that directory to store all the bash scripts as we progress practicing with the example programs in the following chapters to come.
To save the script as a file, use the following extension .sh. For example, creating a hello world bash file would result in hello.sh.
To save the script as a file, use the following extension .sh. For example, creating a hello world bash file would result in hello.sh.
Hello World
Like those days where it’s a tradition to at least demonstrate a hello world program as the pupil embarks on her new programming language journey, below is a simple hello world program written in BASH:
#!/bin/bash echo Hello World!
The first line tells the computer to invoke the bash interpreter and the second line is the statement that will be printed on the terminal screen.
Note that you have to type and save the above code to a file ending with the extension .sh. Simply typing those lines on the terminal won’t work.
Conclusion
BASH is a scripting language and is the default shell interpreter on most Linux distributions. It is mainly used by software developers and system admins for the purpose of automation. And of course hobbyists too 🙂 I hope you had a good read. I’ll see you in the next chapter where we will discuss variables in BASH. Until next time, stay tuned.