Bash Scripting for Beginners

Get acquainted with bash scripting in this course for beginners.

Shell is the core part of Linux. It allows you to interact with the Linux kernel by using various commands like cd, ls, cat etc.

Bash is one of the many available shells for Linux. They have mostly common but not identical syntax. Bash is the most popular shell out there and is the default on most Linux distributions.

You open a terminal or SSH session and you have a shell running in it even if you cannot really visualize it.

linux terminal introduction

When you type a command, it is interpreted by the shell. If the command and syntax are correct, it will be executed otherwise you'll see an error.

Why bash scripts when you can just run Linux commands?

You can enter the commands directly in the terminal and they will be executed.

abhishek@itsfoss:~$ echo "hello world"
hello world

And the same can be done in a script as well:

abhishek@itsfoss:~$ cat >> script.sh
#!/bin/bash

echo "hello world"
abhishek@itsfoss:~$ bash script.sh
hello world

Why do you need shell scripts then? Because you don't have to type the same command again and again. You just run the shell script.

Also, if you have complicated logic in your script, typing it all in the terminal won't be a good idea.

For example, if you enter the command below, it will work. But it is not easy to understand and typing it again and again (or even searching for it in the bash history) is a pain.

if [ $(whoami) = 'root' ]; then echo "root"; else echo "not root"; fi

Instead, you can put in a shell script so that it is easier to understand and run it effortlessly:

#!/bin/bash

if [ $(whoami) = 'root' ]; then
	echo "You are root"
else
	echo "You are not root"
fi

This was still simple. Imagine a complicated script with fifty or a hundred lines!

What will you learn?

There are nine sections in this bash scripting tutorial. You'll learn to:

  • Create and run your first bash shell script
  • Use variables
  • Pass arguments and accept user inputs in your bash scripts
  • Perform mathematical calculations
  • Using arrays in Bash scripts
  • Manipulate strings
  • Use conditional statements like if-else
  • Use for, while and until loops
  • Create functions
πŸ’‘
All these chapters also contain practice exercises.

Who is the target audience?

Anyone who wants to start learning bash shell scripting.

If you are a student with shell scripting as part of your course curriculum, this series is for you.

If you are a regular desktop Linux user, this series will help you understand most shell scripts you come across while exploring various software and fixes. You could also use it to automate some common, repetitive tasks.

Basically, Bash should be on the learning roadmap of any serious Linux user.

By the end of this Bash Basics series, you should be able to write simple to moderate bash scripts.

All the chapters in the series have sample exercises so you can learn it by doing it.

πŸ—’οΈ
You'll learn bash shell scripting here. While other shells have mostly the same syntax, their behavior still differs at a few points. Bash is the most common and universal shell, so start learning shell scripting with bash.

Prerequisites

🚧
It is desirable that you have basic knowledge of the Linux command line and any programming language.

If you are absolutely new to the Linux command line, I advise you to get the basics right first.

19 Basic But Essential Linux Terminal Tips You Must Know
Learn some small, basic but often ignored things about the terminal. With the small tips, you should be able to use the terminal with slightly more efficiency.

You should understand how to go to a specific location in the command line. For that, you need to understand how path works in the Linux filesystem works.

Absolute vs Relative Path in Linux: What’s the Difference?
In this essential Linux learning chapter, know about the relative and absolute paths in Linux. What’s the difference between them and which one should you use.

Next, this tutorial series gives you the basic of directory navigation and file manipulation.

Getting Started With Linux Terminal
Want to know the basics of the Linux command line? Here’s a tutorial series with a hands-on approach.

Let's begin!

About the author
Abhishek Prakash

Abhishek Prakash

Created It's FOSS 11 years ago to share my Linux adventures. Have a Master's degree in Engineering and years of IT industry experience. Huge fan of Agatha Christie detective mysteries πŸ•΅οΈβ€β™‚οΈ

Become a Better Linux User

With the FOSS Weekly Newsletter, you learn useful Linux tips, discover applications, explore new distros and stay updated with the latest from Linux world

itsfoss happy penguin

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to It's FOSS.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.