First Bash Script

Home First Bash Script
By: ankits / July 20, 2023


Bash scripting is an essential skill for any Linux user. Whether you are a developer or a system administrator, knowing how to write and execute Bash scripts can help you automate routine tasks and improve your productivity. In this blog post, we’ll cover the basics of Bash scripting in Linux and some best practices for writing efficient and reliable scripts.

Bash is a Unix shell that provides a command-line interface for interacting with the operating system. Bash scripting is the process of writing scripts that automate repetitive tasks by executing commands in a predefined order. The Bash script is a plain text file with a “.sh” extension, which contains a sequence of Bash commands and instructions.

To create a Bash script, you can use any text editor that is available on your Linux system. It’s best to use a text editor that provides syntax highlighting and auto-completion to make the coding process easier. Once you’ve written the script, you need to make it executable using the chmod command. For example, if your script file is named myscript.sh, you can make it executable with the command chmod +x myscript.sh

The first line of the script is called the “shebang,” which tells the operating system which interpreter to use to execute the script. For a Bash script, the shebang line should be “#!/bin/bash.” The shebang line should be the first line of the script, and there should be no blank lines before it.

In Bash scripting, variables are used to store values that can be referenced later in the script. You can create a variable by assigning a value to it using the = sign. For example, to create a variable named “myvar” with the value “hello,” you can use the command “myvar=hello” To reference the value of a variable, you can use the “$” sign followed by the variable name. For example, to print the value of the “myvar” variable, you can use the command echo $myvar

Bash scripts can use conditional statements to execute different commands based on the result of a test. The if statement is used to execute a block of code if a condition is true. For example, to check if a file exists, you can use the following code:COPY

if [ -e /path/to/file ]; then
  echo "File exists."
else
  echo "File does not exist."
fi

The “while” statement is used to execute a block of code repeatedly as long as a condition is true. For example, to print the numbers from 1 to 10, you can use the following code:COPY

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

And yes, we have symbols for greater than (>), less than (<), and equals to (==) sign, and they are:

  • -eq for comparison
  • -ge for Greater than comparison
  • -le for less than comparison

Bash scripts can also use functions to group a set of commands into a reusable block of code. Functions are defined using the “function” keyword followed by the function name and the commands to be executed. For example, to define a function named “myfunc” that prints a message, you can use the following code:COPY

function myfunc {
  echo "Hello, world!"
}

To call a function, you can use the function name followed by parentheses. For example, to call the “myfunc” function, you can use the command myfunc().

In Bash scripting, error handling is essential to ensure that your script behaves as expected even when unexpected conditions occur. But that is outside the scope of this article.

Thanks for reading! Follow me for more! ✨

Previous post
Ansible – an Introduction

Leave a Comment

Copyright © 2022. Designed by WordPressRiver