Bash Scripting 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.

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 worldAnd the same can be done in a script as well:
abhishek@itsfoss:~$ cat >> script.sh#!/bin/bashecho "hello world"abhishek@itsfoss:~$ bash script.shhello worldWhy 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"; fiInstead, you can put in a shell script so that it is easier to understand and run it effortlessly:
#!/bin/bashif [ $(whoami) = 'root' ]; thenecho "You are root"elseecho "You are not root"fiThis 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
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 thelearning 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.
Prerequisites
If you are absolutely new to the Linux command line, I advise you to get the basics right first.
It's FOSSAbhishek Prakash
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.
Linux HandbookAbhishek Prakash
Next, this tutorial series gives you the basic of directory navigation and file manipulation.
It's FOSSAbhishek Prakash
Let's begin!
Let's learn tocreate your hello world bash shell script and run it.
It's FOSSAbhishek Prakash
Abhishek Prakash
Created It's FOSS 13 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 🕵️♂️

8 Years Later, Linux-based AsteroidOS 2.0 is Here to Add New Life to Your Old Smartwatch

Linux Kernel 6.19 Arrives! You Get Intel, AMD Improvements, and ASUS Armoury Support

Not Kidding! Bash Shell Manual is Part of Epstein Files 🫣

36 Years in Making, GNU's Very Own Kernel Project Hurd is Anything But Dead

Windows 11 May Introduce a Linux-like Feature for its Power Users

FOSS Weekly #26.08: KDE Plasma 6.6, Mint Release Schedule Change, ASCII Weather, Firefox Tweaking and More Linux Stuff

This is Probably the Best Video Downloader App (And it is Free and Open Source)

KDE Plasma 6.6 Turns Spectacle Into an OCR Tool and Ships a New Setup Wizard
Become a Better Linux User
Support Independent Content
We respect your choice to use an ad blocker! It's FOSS is an independent publication that relies on your support.
Consider supporting us to keep quality Linux content free for everyone.

