Post

The Journey: Bash Shell Basics and Examples

What a Bash Shell

Bash (Bourne Again SHell) is one of the most popular and widely used Unix shells on Linux operating systems. This shell provides a command-line interface that allows users to execute commands, scripts, and programs efficiently.

Installing Bash

On most Linux distributions, Bash is installed by default. To check if Bash is installed, you can run the following command:

1
bash --version

If Bash is installed, you will see the output of the Bash version installed on your system.

Running Basic Commands

Here are some basic commands that are commonly used in Bash:

1. ls - List Files and Directories

The ls command is used to list files and directories within the current directory.

1
ls

2. cd - Change Directory

The cd command is used to change the current working directory.

1
cd /path/to/directory

3. pwd - Print Working Directory

The pwd (print working directory) command is used to display the current working directory.

1
pwd

4. echo - Display Message in Terminal

The echo command is used to display a message or variable in the terminal.

1
echo "Hello, World!"

5. touch - Create an Empty File

The touch command is used to create a new empty file or update the timestamp of an existing file.

1
touch newfile.txt

Variables in Bash

Variables in Bash are used to store data that can be reused. To define a variable, simply use the = sign without spaces.

1
2
NAME="Bash"
echo "Welcome to $NAME shell"

Writing and Running Bash Scripts

A Bash script is a text file containing Bash commands that can be executed. These scripts typically have a .sh extension.

Simple Bash Script Example

Create a file named hello.sh and add the following lines:

1
2
3
4
#!/bin/bash
# Simple script to display a message

echo "Hello, World!"

To run this script, follow these steps:

  1. Make the script executable using the chmod command:

    1
    
     chmod +x hello.sh
    
  2. Run the script:

    1
    
     ./hello.sh
    

The output will be:

1
Hello, World!

Control Flow in Bash

Bash supports various control flow structures like if, for, and while.

if Statement Example

1
2
3
4
5
6
7
8
9
10
#!/bin/bash
# Example of an if statement

NUMBER=10

if [ $NUMBER -gt 5 ]; then
    echo "Number is greater than 5"
else
    echo "Number is not greater than 5"
fi

for Loop Example

1
2
3
4
5
6
#!/bin/bash
# Example of a for loop

for i in 1 2 3 4 5; do
    echo "Number: $i"
done

while Loop Example

1
2
3
4
5
6
7
8
9
#!/bin/bash
# Example of a while loop

COUNTER=0

while [ $COUNTER -lt 5 ]; do
    echo "Counter: $COUNTER"
    COUNTER=$((COUNTER + 1))
done
This post is licensed under CC BY 4.0 by the author.