Bash Scripting Tutorial: A Beginner’s Guide to Linux Shell Scripting and Command Line

Estimated read time 3 min read

Introduction to Bash Scripting and Shell Scripts

Bash (Bourne Again SHell) is a widely used shell and command language in Linux and Unix-based operating systems. Bash scripting allows users to automate tasks, write complex command sequences, and create shell scripts to execute a series of commands. In this tutorial, we will explore the fundamentals of Bash scripting, covering the basics of writing shell scripts and executing them on the command line.

Getting Started with Bash Scripting

Understanding the Shell Environment

The shell is a command interpreter that provides an interface to interact with the operating system. In a Bash shell, you can type commands and execute them by pressing the Enter key. The shell then interprets the commands and performs the requested operations.

Creating and Executing a Bash Script

To create a Bash script, open a text editor and save the script with a .sh extension. For example, The first line of the script should begin with a shebang (#!) followed by the path to the Bash interpreter:


After the shebang line, you can write your desired commands using the Bash scripting syntax. For example:


# Print a greeting message
echo "Hello, world!"

# List files in the current directory

Save the script and make it executable by running the following command:

chmod +x

To execute the script, use the following command:


The script will run, and you will see the output in the terminal.

Variables and User Input

Using Variables

Variables in Bash are used to store data and provide a way to access and manipulate it. To assign a value to a variable, use the following syntax:


Here’s an example:


# Assign a value to a variable

# Use the variable in a command
echo "Hello, $name!"

User Input

Bash allows you to prompt the user for input using the read command. The read command reads a line of text from the user and stores it in a variable. Here’s an example:


# Prompt the user for their name
echo "Enter your name:"
read name

# Print a greeting message
echo "Hello, $name!"

Conditional Statements

if-else Statement

The if-else statement in Bash allows you to perform conditional execution of commands based on a specified condition. The basic syntax is as follows:

if condition
    # commands to execute if the condition is true
    # commands to execute if the condition is false

Here’s an example:


# Prompt the user for their age
echo "Enter your age:"
read age

# Check if the age is greater than or equal to 18
if [ $age -ge 18 ]
    echo "You are an adult."
    echo "You are a minor."


for Loop

The for loop in Bash allows you to iterate over a sequence of values or a list. The basic syntax is as follows:

for variable in list
    # commands to execute for each value
Mark Stain

My name is Mark Stein and I am an author of technical articles at EasyTechh. I do the parsing, writing and publishing of articles on various IT topics.

You May Also Like

More From Author

+ There are no comments

Add yours