Learn Bash Scripting: A Beginner's Guide to Loops, Conditionals, and Functions

Posted
Comments 0

Bash is a popular Unix shell that provides a command line interface to interact with the operating system. In addition to executing commands, Bash can be used to write scripts that automate tasks, manipulate files and directories, and perform other system administration tasks. In this tutorial, we will cover the basic programming concepts of Bash, including loops, conditionals, and functions.

Contents

I can highly recommend these books to help you learn Bash and also as handy Bash reference.

Prerequisites

To follow this tutorial, you will need a Unix-based operating system with Bash installed. This includes Linux, macOS, and many other Unix-like systems. You will also need a text editor to create and edit Bash scripts.

Creating a Bash script

To create a Bash script, you can use any text editor, such as nano, vim, or Sublime Text. The script file should have an .sh extension, which indicates that it is a Bash script. For example, you can create a new file called myscript.sh by running the following command:

nano myscript.sh

Once you have created the file, you can start writing your Bash script.

Basic programming concepts

Variables

In Bash, variables are used to store values that can be used later in the script. A variable is defined by assigning a value to a name. For example, you can define a variable called myvar and assign it the value Hello, World! like this:

myvar="Hello, World!"

You can then use the variable in the script by enclosing it in curly braces, like this:

echo ${myvar}

This will print Hello, World! to the terminal.

Loops

Loops are used to repeat a set of commands multiple times. Bash provides two types of loops: for and while.

For loop

A for loop is used to iterate over a set of values. For example, the following script will print the numbers 1 through 5 to the terminal:

for i in 1 2 3 4 5
do
  echo $i
done

While loop

A while loop is used to repeat a set of commands as long as a condition is true. For example, the following script will print the numbers 1 through 5 to the terminal:

i=1

while [ $i -le 5 ]
do
  echo $i
  i=$((i+1))
done

This script uses the -le operator to test whether i is less than or equal to 5. The i=$((i+1)) line increments the value of i by 1 each time the loop runs.

Conditionals

Conditionals are used to test whether a condition is true or false, and execute different commands based on the result. Bash provides several conditional statements, including if, elif, and else.

If statement

An if statement is used to execute a set of commands if a condition is true. For example, the following script will print Hello to the terminal if the variable name is equal to Alice:

if [ $name = "Alice" ]
then
  echo "Hello"
fi

If-else statement

An if-else statement is used to execute one set of commands if a condition is true, and another set of commands if it is false. For example, the following script will print Hello to the terminal if the variable name is equal to Alice, and Goodbye if it is not:

if [ $name = "Alice" ]
then
  echo "Hello"
else
  echo "Goodbye"
fi

If-elif-else statement

An if-elif-else statement is used to test multiple conditions and execute different sets of commands based on the result. For example, the following script will print Hello to the terminal if the variable name is equal to Alice, Goodbye if it is equal to Bob, and Nice to meet you if it is anything else:

if [ $name = "Alice" ]
then
  echo "Hello"
elif [ $name = "Bob" ]
then
  echo "Goodbye"
else
  echo "Nice to meet you"
fi

Functions

Functions are used to group a set of commands together and execute them as a single unit. In Bash, functions are defined using the function keyword, followed by the function name and a set of parentheses. For example, the following script defines a function called greet that takes one argument and prints a greeting to the terminal:

function greet {
  echo “Hello, $1”
}

greet “Alice”

This will print Hello, Alice to the terminal.

Conclusion

In this tutorial, we covered the basic programming concepts of Bash, including variables, loops, conditionals, and functions. Bash is a powerful tool for automating tasks and performing system administration tasks on Unix-based operating systems. With these basic concepts, you can start writing your own Bash scripts to automate your workflow and make your life easier.

Newsletter Signup







Privacy Policy

See Also

Further Reading

Author
Categories Bash

Comments

There are currently no comments on this article.

Comment

Enter your comment below. Fields marked * are required. You must preview your comment before submitting it.





PLEASE NOTE: You must preview a comment before submitting

Comments use Textile formatting

Update cookies preferences