Introduction to bash scripting

Bash scripting is a powerful tool for automating repetitive tasks and processes on Linux and Unix-based systems. Bash scripts are essentially a series of commands that can be executed in sequence, allowing you to automate tasks such as data processing, system administration, and more. In this tutorial, we will go through the basics of creating and running a bash script.

Create a new file with the .sh extension. For example,

Open the file in a text editor and add your commands. Make sure to use the correct syntax, as bash scripts are very picky about syntax and formatting.

Make the file executable by running the command chmod +x This will give the file the necessary permissions to be executed.

Run the script by typing ./ in the terminal. This will execute the commands in the script in the order they are written.

Here's an example of a simple bash script that prints "Hello, World!" to the terminal:


echo "Hello, World!"

The first line, #!/bin/bash, is called the shebang and is used to specify which interpreter to use for running the script. In this case, we're using the bash interpreter. You can also use variables in your script. For example:


name="John Doe"

echo "Hello, $name"

This script will print "Hello, John Doe" to the terminal. Variables in bash scripts are defined using the = operator, and they can be used by placing a $ symbol in front of the variable name.

You can also use conditional statements, loops, and functions in your script to make it more powerful and flexible. Conditional statements are used to perform different actions based on different conditions. For example:


if [ $1 -gt 100 ]


echo "That's a large number."


This script will check if the first command line argument is greater than 100, and if it is, it will print "That's a large number." to the terminal.

Loops are used to repeat a certain action multiple times. The most commonly used loops in bash are for, while, and until. For example:


for i in {1..5}


echo "Iteration $i"


This script will print "Iteration 1", "Iteration 2", "Iteration 3", "Iteration 4", and "Iteration 5" to the terminal, as it runs the echo command five times.

Functions are used to group a set of commands together and can be called multiple times within the script. For example:


greet() {

echo "Hello, $1"


greet "John"

greet "Jane"

This script will print "Hello, John" and "Hello, Jane" to the terminal.

You can also use command line arguments in your script, for example:


echo "Hello, $1"

You can pass an argument when you run the script like this: ./ John

This script will print "Hello, John" to the terminal. Command line arguments can be accessed in the script using the special variables $1, $2, $3 and so on.

You can also use built-in commands like ls, mkdir, rm, cp, mv, echo and many more to automate file and folder operations. For example, you can use the find command to search for files with a certain name and the sed command to search and replace text in a file.

There are many other things you can do with bash scripting.

No comments:

Post a Comment