DEV Community

Cover image for Understanding Bash-scripting
Favour Onyeneke
Favour Onyeneke

Posted on

Understanding Bash-scripting


Table Of Contents

  • What is Bash and bash-script

  • Advantages of bash-scripting

  • Basic structure of a bash-script (How do they work and how do we run them?)

  • The Shebang (#!)

  • Comments

  • Variables

  • Basic commands

  • Control structures

WHAT IS BASH AND BASH-SCRIPT

Bash stands for "Bourne Again SHell." It is a command language interpreter that processes commands typed into a terminal or stored in a script file.

A bash script is a file containing a sequence of commands that are executed by the bash program line by line. They are basically scripts written to automate a process.

Bash scripting is a powerful way to automate tasks, manage system operations, and handle files on Unix-based systems, including Linux and macOS. Here’s a beginner-friendly guide to help you understand the basics of bash scripting:

ADVANTAGES OF BASH SCRIPTING

Automation:
Bash scripts are a powerful tool for automating repetitive tasks, helping to save time and minimize the potential for errors. By utilizing them, you can simplify your workflow and enhance efficiency.

Integration:
Bash scripts integrate easily with other tools and software, allowing them to call programs, execute commands, and process data seamlessly..

Ease of Use:
Bash is easy to learn and use, especially if you know Unix or Linux commands. The scripts you write in Bash are clear and easy to read.

Cost-Effective:
Bash is a free and open-source tool. It is a cost-effective choice for many administrative tasks.

Flexibility:
Bash scripts can perform a wide range of tasks, including simple file operations and complex system administration tasks.

Rapid Development:
Scripts can be written and modified quickly, allowing for rapid development and deployment.

BASIC STRUCTURE OF BASH-SCRIPT

How Bash scripts work

Script File:A bash script is a text file that consists of a series of commands designed to be executed by the Bash shell. This allows users to automate tasks and streamline processes efficiently.

Shebang Line: The script typically starts with a shebang line (#!/bin/bash) that tells the system which interpreter to use for the script.

Commands and Logic: The script includes Unix commands, control structures (like loops and conditionals), and sometimes even functions.

Execution Flow: When you run the script, the shell interpreter reads and executes each command in order.

RUNNING A BASH-SCRIPT

1.Create the script file
Conventional standards demands that you save every bash-script file name with a .sh extension

a script file

2.Open a text editor and type your script using either vim or nano. Here's a simple example:

your script
Exit your editor.

3.Make the Script Executable
Open your terminal and navigate to the directory where your script is saved.

Use the chmod command to make your script executable:

file permission
The user now has the execute permission on the file "myscript.sh".

4.You can run your script by typing ./ and the file name

run your script
Alternatively, you can run the script without changing file permission by invoking the bash interpreter directly.

script

Let's create a more detailed example that demonstrates a few basic
features of bash scripting

A basic script

Code successfully executed!

code executed

Code Breakdown

Shebang Line:
{#!/bin/bash}

This line indicates that the script should be run using the Bash shell.

Variable Assignment:
name="Alice"
age=30

Two variables are assigned: name is set to "Alice", and age is set to 30.

Function Definition:
greet_user() {

echo "Hello, \$1! Welcome to the bash scripting tutorial."}

A function named greet_user is defined. It takes one argument (represented as \$1) and prints a greeting message.

Conditional Statement:
if [ $age -ge 18 ]; then

echo "$name is an adult."

else

echo "$name is not an adult."

fi

This conditional checks if age is greater than or equal to 18. If true, it prints that Alice is an adult; otherwise, it states she is not.
Loop:
echo "Counting from 1 to 5:"

for i in {1..5}; do
echo "Number $i"

done

This loop counts from 1 to 5 and prints each number.

Function Call:
greet_user$name

The greet_user function is called with name ("Alice") as its argument, which triggers the greeting message.
End of Script:
echo "Done!!!"

A message indicating the completion of the script's execution is printed.

THE SHEBANG

The shebang is the first line in a script, typically written as #!/bin/bash for Bash scripts.

The shebang tells the operating system which interpreter to use to execute the script. In this case, #!/bin/bash specifies that the Bash shell should be used.
#!bin/bash
This indicates that the script is intended to be executed using the Bash shell, allowing it to run any Bash-specific features or syntax contained within.

COMMENTS

Comments in Bash scripting are important for documentation, making code easier to understand, and for temporarily disabling code without deleting it.

Use the # symbol to indicate the start of a comment. Everything following the # on that line will be ignored by the interpreter.

VARIABLES

In Bash scripting, variables are used to store data that can be referenced throughout the script.
variable_name="value"

BASIC COMMANDS

Bash scripting offers a variety of commands to perform different tasks. Here’s an overview of some essential basic commands commonly used in Bash scripting:
These basic commands form the foundation of Bash scripting, allowing you to create powerful scripts for automation and system tasks.

  • Echo The echo command is used to display text or variables in the terminal.
  • Read The read command takes user input and stores it in a variable.
  • ls Lists files and directories in the current directory.
  • exit Exits the script or the shell with a given exit status.
  • export Sets an environment variable.

CONTROL STRUCTURES

Control structures in Bash scripting allow you to manage the flow of execution in your scripts, enabling conditional execution, looping, and branching based on different conditions. Here’s a detailed overview of the primary control structures you can use in Bash:

  1. Conditional Statements
    If Statement
    The if statement evaluates a condition and executes a block of code if the condition is true.

  2. Logical Operators
    You can combine conditions using logical operators:
    && (AND)
    || (OR)

  3. Case Statement
    The case statement can replace complex if statements when checking a variable against multiple patterns.

  4. Loops
    Loops allow repeated execution of a block of commands.
    For Loop
    The for loop iterates over a list of items or a sequence.

While Loop
The while loop continues until a specified condition is false.

Until Loop
The until loop is similar to while, but it continues until the condition becomes true.

  1. Nested Control Structures You can nest if statements and loops within each other to handle more complex scenarios. Control structures in Bash scripting are essential for making scripts dynamic and responsive to input and conditions. By utilizing conditional statements (if, case), loops (for, while, until), and logical operators, you can create sophisticated scripts that automate tasks efficiently. As you become more comfortable with these structures, you'll find how they greatly enhance the flexibility and capability of your Bash scripts.

Top comments (0)