DEV Community

Cover image for Learn Bash Scripting in 10 minutes🧙‍♂️🪄
Arindam Majumder
Arindam Majumder Subscriber

Posted on

Learn Bash Scripting in 10 minutes🧙‍♂️🪄

Introduction

As a Programmer, learning bash scripting is really important. It allows us to write scripts to automate boring and repetitive tasks.

The applications and uses of scripting are numerous. From managing system processes to performing complex data manipulations, bash scripting helps us to do everything efficiently.

In this next 10 minutes (yes, just 10!), I'll cover the basics of bash scripting with examples to help you streamline your workflow.

Sounds intesting?

GIF

So, Grab your favorite drink, and let's start scripting!

Prerequisites

Before we dive deep into it, here's what you'll need:

  1. A Linux or macOS system (Windows users can use WSL)

  2. Any text editor (we'll use Vim here)

  3. Curiosity and a willingness to experiment

That's it! : )

Basics

In this section, we'll understand the basic commands of bash scripting. These commands we'll use in most of the places and they are pretty easy to understand, so take a look at them and you'll be ready to go!

At first, let's create a new folder (also called a directory) where we can keep our files. To do this, we use the mkdir command, which stands for "make directory." Here's how you use it:

mkdir <directory name>
Enter fullscreen mode Exit fullscreen mode

This command will create a new folder/directory in your current location.

Now, as we have the directory, let's get into it. To go inside the directory, we'll use the cd command that stands for "change directory":

cd <directory name>
Enter fullscreen mode Exit fullscreen mode

Terminal ScreenShot

As we can see in the above image, we're inside the the directory "Arindam"

But what if you want to go back to where you were before?

No problem!

Use cd .. to go back to the parent directory:

cd ..
Enter fullscreen mode Exit fullscreen mode

Terminal ScreenShot

Now, if we want to see the files amd folders inside our current directory, we use the ls command:

ls
Enter fullscreen mode Exit fullscreen mode

This will list all the files and folders in your current directory:

Terminal ScreenShot

Let's create a new file. We can do this with the touch command:

# syntax: touch <filename>

touch my_first_file.txt
Enter fullscreen mode Exit fullscreen mode

This creates an empty file named "my_first_file.txt" in our current folder.

Sometimes, we might forget which directory we're working in.

Don't worry!

To get the current working directory we can use the pwd command, which stands for "print working directory":

pwd
Enter fullscreen mode Exit fullscreen mode

This will show you the full path to your current location.

Terminal ScreenShot

Lastly, let's print something to the screen. We use the echo command for this:

echo "Hello World"
Enter fullscreen mode Exit fullscreen mode

This will display "Hello, Bash World!" in your terminal:

Terminal ScreenShot

And with that, we have learnt the basics of Bash scripting!

Easy Right?

Things will get more easier as we'll keep using these in the followig sections as well.

Writing First Bash script

As Developers, we mostly use VS Code or other text editors to write code. But for Writing bash scripts its preferred to use terminal based editors like vim,neovim.

For this guide, I'll be using vim.

Note: If you struggled to exit vim for the first time, don't worry much. We all were there once : )

# Command: vim <file name>

vim hello.txt
Enter fullscreen mode Exit fullscreen mode

this will create a hello.txt file and open with vim. You'll get the following interface:

Terminal ScreenShot

But Wait! We can't write anything here!!

Why?

Simply because, by default vim opens a file in read-only mode. To be able to write on it Press "i" and then it will start the insert mode, with which now we can write on the file:

Terminal ScreenShot

Now, How to save this file?

This is straightforward!

At first, we'll get out of the insert mode by pressing esc then we'll simply press :w

Terminal ScreenShot

This will update the changes and save it:

Terminal ScreenShot

Now we have to get out of vim where many beginners struggle.

Well, not anymore.

To exit the vim editor just write :q and we will get out of our vim editor.

Terminal ScreenShot

Pro tip: you can combine these commands with :wq to save and quit in one go.

Also, if we want to print the content of the file we'll use

# command: cat <file name>
cat hello.txt
Enter fullscreen mode Exit fullscreen mode

And we'll get the content of the file hello.txt that we've just created

Terminal ScreenShot

Now, let's create our first bash script.

Create a file firstCommand.sh and open it with vim

vim firstCommand.sh
Enter fullscreen mode Exit fullscreen mode

In this file, let's add the following:

Terminal ScreenShot

And save this file with :wq

#!/bin/bash is the way to tell the shell script which interpreter to use.

Now, we'll the our first shell script.

bash firstCommand.sh
Enter fullscreen mode Exit fullscreen mode

we'll get the following:

Terminal ScreenShot

But if we try to execute the file, we'll get permission denied error.

Terminal ScreenShot

In Linux, we need to give file permissions to act as an executable file. We'll give the permission with the following command:

chmod +x firstCommand.sh
Enter fullscreen mode Exit fullscreen mode

This will make our file firstCommand.sh executable. Now if we execute the file, we'll get:

Terminal ScreenShot

Now it works! With this we have created our First Bash Script!!

Variables

In Bash we can use variables as well.

Suppose we are copying something from one location to another location. the general approach will be:

cp my/location/path/from my/location/path/to
Enter fullscreen mode Exit fullscreen mode

Now, if we use variables, it will be:

LOC=my/location/path

cp LOC/from LOC/to
Enter fullscreen mode Exit fullscreen mode

This is more readable, isn't it?

And bonus: if you need to change the path later, you only have to change it in one place!

Positional Arguments

Now, we'll learn Positional Arguments, as the name suggests it takes the arguments from a specific positions.

Positional Arguments

We write the arguments after our script as arguments and access them based on their positions.

Let's understand this with an example, We'll create a new script that will take two positional arguments and print them.

create a New file with the following command:

vim PosArg.sh
Enter fullscreen mode Exit fullscreen mode

And add the following code.

Terminal ScreenShot

Look Carefully, here We are taking the positional Arguments with $1 and $2

Next, save this and make this executable( As we have done in the previous sections).

After that, Let's run the executable with two arguments!

Terminal ScreenShot

And we got our expected result. This is how we can use positional arguments in our bash scripts.

Piping

One of the most used features of bash is Piping, it makes is very easy to perform a specific action on the output of another command.

For example, the output of a command is very long and you want to filter that.

Instead of copying that output and pasting in another command we can use piping to do the same thisn in one command.

Let's understand how piping works!

We'll list the contents of a bin directory in long format with the following command:

ls -l /usr/bin
Enter fullscreen mode Exit fullscreen mode

We'll get a whole lot of files. See the screenshot below.

Terminal ScreenShot

As you can see the output is very long and we can't directly copy this outout for piping.

Here, we will use piping to filter only bash files from the output. Let's update the command to this:

ls -l /usr/bin | grep bash
Enter fullscreen mode Exit fullscreen mode

grep is used to search for specific patterns within files/folders.

Now it will only show the bash files:

Terminal ScreenShot

This is How it simplifies

Input/Output Redirection

Alright, let's explore the wonderful world of Input/Output Redirection in Bash. When you learn how to do this, you'll be able to control where your computer sends information, just like magic!

With input redirection, we can take inputs from files instead from the keyboard.

For example, if we want to filter an word from a file, we can simply do that by input redirection. We'll pass the file as the input.

grep "example" < input.txt
Enter fullscreen mode Exit fullscreen mode

This will give us the occurrence of the word example in input.txt

If we want to supply a multiple line of input to a command, we can use << . When we'll write the << operator we'll immediately follow it up with a word that will open and close the text we want to input.

Many people write EOF to start and end that, but that's not mandatory, you can use whatever you want. In this example we'll use the EOF for simplicity.

cat << EOF
> Hey Everyone
> Hope your'e liking this guide
> if so, feel free to share your thoughts
> Thanks
> EOF
Enter fullscreen mode Exit fullscreen mode

Now, we'll see the text written between EOF in the terminal:

Terminal ScreenShot

Now, let's flip the script and talk about output redirection. This is where things get really fun!

Output redirection allows us to send command output to files instead of the terminal. For example, if we want to put the output of echo "hello world" command to a file hello.txt , run the following command.

echo "hello world" > hello.txt
Enter fullscreen mode Exit fullscreen mode

That > symbol is like a funnel, it directs the output into a file instead of printing it out in the terminal.

Now let's see the hello.txt file. For that, Run

cat hello.txt
Enter fullscreen mode Exit fullscreen mode

We'll see the following:

Terminal ScreenShot

Similarly, let's try to add another output to the hello.txt . Run the following command:

echo "Hello Arindam, How are you?" > hello.txt
Enter fullscreen mode Exit fullscreen mode

Now if we see the contents of hello.txt , we'll see it has overwritten the file:

Terminal ScreenShot

So, If we need to append output to the file, we need to use >> instead of >

Let's try to append an output to the hello.txt with >> :

echo "Follow me on Twitter: @Arindam_1729" >> hello.txt
Enter fullscreen mode Exit fullscreen mode

Now, we'll see the text is appended to the previous text of hello.txt

Terminal ScreenShot

This can be really helpful if you want to log your outputs in a file. also to keep your errors as well.

If/else statements

Just like other programming languages, we can write if else statements in Bash as well.

The syntax of it is:

if (condition);then
 statement
elif(condition);then
 statement
else
 statement
fi
Enter fullscreen mode Exit fullscreen mode

Let's try to use this with an example

We'll create a script that will take the user's name, if the username is Arindam, then it will print Welcome Admin otherwise, it'll print "You're not an Admin, login as Admin".

#!/bin/bash

if [[ "${1,,}" = "arindam" ]]; then
    echo "Hey Admin, Welcome to our Bash Tutorial."
elif [[ "${1,,}" = "magician" ]]; then
    echo "Hey Magician, You don't have Admin Access"
else
    echo "You Don't have Admin access, login as an Admin"
fi
Enter fullscreen mode Exit fullscreen mode

Now let's try to run this with different arguments:

Terminal ScreenShot

Case Statement

If/else statements are good for checking small conditions. But as the number of conditions increases it's hard to manage them. In that case, we can use Case statements

case EXPRESSION in

  PATTERN_1)
    STATEMENTS
    ;;

  PATTERN_2)
    STATEMENTS
    ;;

  PATTERN_N)
    STATEMENTS
    ;;

  *)
    STATEMENTS
    ;;
esac
Enter fullscreen mode Exit fullscreen mode

Let's understand this with an example, we'll implement the previous example with case statements.

So, for that, the code will be:

#!/bin/bash

case ${1,,} in
        "arindam")
                echo "Hey Admin, Welcome to our Bash Tutorial."
                ;;
        "magician")
                echo "Hey Magician, You don't have Admin Access"
                ;;
        *)
                echo "You Don't have Admin access, login as an Admin"
                ;;
esac
Enter fullscreen mode Exit fullscreen mode

Now, let's execute the code and see the results:

Terminal ScreenShot

Works perfectly. Here it also adds a structure and makes the code more readable for all.

Arrays

We can assign multiple values to a variable collected in a list. we call these variables arrays.

Let's create our first array:

FIRST_ARRAY=(one two three four five six)
Enter fullscreen mode Exit fullscreen mode

Now we'll print the array. For that, let's try to print the variable (normally what we do):

echo $FIRST_ARRAY
Enter fullscreen mode Exit fullscreen mode

Terminal ScreenShot

But, as you can see, we are only getting the first element of the array. In order to print the whole array we need to write it in this way

echo ${FIRST_ARRAY[@]}
Enter fullscreen mode Exit fullscreen mode

Now we can get all the elements of the array:

Terminal ScreenShot

We can also print individual elements based on their indexes. For that, the syntax will be:

# Replace INDEX_OF_THE ELEMENT with the index of the element that you want to print 
echo ${FIRST_ARRAY[INDEX_OF_THE ELEMENT]}
Enter fullscreen mode Exit fullscreen mode

For loop

For loops are a fundamental building block in Bash scripting. They allow us to automate repetitive tasks, process multiple items efficiently. The syntax is:

#!/bin/bash

for n in a b c;
do
   echo $n
done
Enter fullscreen mode Exit fullscreen mode

Now, let's try to print the items of the array that we created previously with a for loop.

 for item in ${FIRST_ARRAY[@]};
 do 
     echo $item; 
 done
Enter fullscreen mode Exit fullscreen mode

This loop iterates through each element of the FIRST_ARRAY. The [@] notation is a parameter expansion that refers to all elements of the array. When used with ${...}, it expands to the full list of array elements

If we execute this we'll get:

Terminal ScreenShot

If you want to remove the new lines, just add -n in the echo comand.

Functions

In some scenarios, we have a lot of code that we want to call multiple times. That's where functions come in handy. They're like little code snippets you can call whenever you need them.

In Bash scripting, functions serve as a powerful tool for organizing and reusing code. It is essentially a set of commands that can be called numerous times.

Here's the syntax of it:

function_name () {
  commands
}
Enter fullscreen mode Exit fullscreen mode

These functions are the little programs within your script that we can run within our script. With this, we can make our code reusable.

Now let's create our first function.

vim bashfn.sh
Enter fullscreen mode Exit fullscreen mode

and add the following script:

#!/bin/bash

first_fn () {
   echo 'This is my first Bash function'
}

first_fn
Enter fullscreen mode Exit fullscreen mode

Now, if we run this script we'll get:

Terminal ScreenShot

We can also pass arguments to the bash function. To pass arguments to a function, we've to add the parameters after the function call.

Let's create a new function in the script and pass it one argument.

#!/bin/bash

first_fn () {
   echo 'This is my first Bash function'
}

first_arg(){
   echo "this is the first argument: $1"
}

first_fn

first_arg Arindam
Enter fullscreen mode Exit fullscreen mode

Now if we run this script, we'll get:

Terminal ScreenShot

AWK

Awk is one of the most useful tools in Bash. We use AWK to filter file contents or output of a command in such a way that we get the essential part of the output and the way we like it.

The basic syntax is:

# This will print the whole content of the file
awk '{ print }' /path/to/file
Enter fullscreen mode Exit fullscreen mode

Let's understand this with an example, we'll filter a word from a text file and print that.

For that first, let's create a text file temp.txt and add texts. we'll use output redirection to create this:

echo Arindam Magician Mohun Bagan Barcelona > temp.txt
Enter fullscreen mode Exit fullscreen mode

Now, we'll use awk to filter text from the text file temp.txt :

If we want to print specific text like the first/second word then we can use $1 and $2

and if we want to print the whole content of the file, we'll keep the print statement {print }

Note: By default, awk uses space character as a separator

Here's the results of this:

Terminal ScreenShot

Instead of the default separator, we can use different characters as separators.

Let's create a CSV file and understand this:

echo Arindam,Magician,Mohun Bagan,Barcelona > temp.csv
Enter fullscreen mode Exit fullscreen mode

Here instead of awk's default separator, we'll use ","

For that, the command will be:

awk -F, {print $3} temp.csv
Enter fullscreen mode Exit fullscreen mode

Here, Now we'll get the third element:

Terminal ScreenShot

If you look closely, the third word is separated by a space but we're not considering that as we've mentioned "," as a separator.

We can also use awk with commands using pipes.

echo "hello Arindam" | awk '{print $2}'
Enter fullscreen mode Exit fullscreen mode

If we run this, we'll get:

Terminal ScreenShot

SED

There are certain times when we want to change a certain value in text files, that's where sed is super helpful.

SED is a command line tool that lets us modify certain values in a text file using regular expressions.

Here's the syntax:

sed 's/text_to_change/to_this_text/g' filename
Enter fullscreen mode Exit fullscreen mode

Let's create a file testsed.txt with some dummy text:

Terminal ScreenShot

Now from that text, we'll replace Messi's word with Arindam.For that, the command will be:

sed 's/messi/arindam/g' testsed.txt
Enter fullscreen mode Exit fullscreen mode

After running this command we'll get:

Terminal ScreenShot

Conclusion

In this Article, We've covered the fundamental concepts, from basic command syntax to more advanced topics like functions.

Hope you found this article useful. If so, feel free to share it with your developer friends!

Also, Follow me For More Content like this:

For Paid collaboration mail me at: arindammajumder2020@gmail.com.

Thank you for Reading till the end.

How do I use the code snippet feature? - Question - Sparkle Community

Top comments (11)

Collapse
 
ddebajyati profile image
Debajyati Dey

Wow you wrote the tutorial in a playful and interactive manner!
Interesting....

Great work Arindam!

Collapse
 
arindam_1729 profile image
Arindam Majumder

Thanks a lot for checking out Debajyati!

Collapse
 
tanmoys95268896 profile image
Tanmoy Sinha

Great Share!

Collapse
 
arindam_1729 profile image
Arindam Majumder

Thanks for checking out Tanmoy!

Collapse
 
akshaybond30160 profile image
Akshay bondre

Very Detailed guide, Arindam!

Collapse
 
arindam_1729 profile image
Arindam Majumder

Glad you liked it!

Collapse
 
akshaycodes profile image
Akshay SIng

Thanks for sharing Arindam!

Collapse
 
arindam_1729 profile image
Arindam Majumder

Glad you liked it!

Collapse
 
hemath923604 profile image
Hemath

Well Written Arindam!

Collapse
 
arindam_1729 profile image
Arindam Majumder

Glad you liked it Hemanth!

Collapse
 
sabrinaesaquino profile image
Sabrina

Amazing post Arindam!