Linux Bash Scripting for Beginners – Shell Script and Command Line Part I

Shell scripting has a major role in Linux process automation. To do this, generally we create a file with a list of commands that can be run simultaneously.

We’ll start with the fundamentals of Linux bash scripting for beginners in this post, covering variables, commands, inputs and outputs, and debugging. Along the road, we’ll witness instances of each as well.

Pre-requisite

You will need to have the following resources in order to follow along with this tutorial:

  • Linux operating system that is operational and has command line access. If required any support for Linux OS Installation follow the post How to Install Rocky Linux 8

Introduction

A file with a series of commands that the bash program runs through line by line is called a bash script. It enables you to carry out an array of tasks, like using the command line to start a process, create a folder, and go to a particular location. These commands can be saved as a script, which allows you to run the script and perform the same set of actions repeatedly.

Benefits of Using Bash

For controlling system resources, automating system administration operations, and carrying out other standard tasks in Unix/Linux systems, bash scripting is a strong and adaptable tool. Shell scripting has the following benefits:

  • Automation: By using shell scripts, you may save time and lower the possibility of errors that might arise from manual execution of repetitive operations and processes.
  • Portability: By using emulators or virtual machines, shell scripts can be executed on a number of platforms and operating systems, including Windows, Linux, macOS, and Unix.
  • Flexibility: Shell scripts are very adaptable and simple to adjust to meet particular needs. To develop scripts with greater capability, they can also be integrated with other programming languages or utilities.
  • Accessibility: Shell scripts don’t require any specialized software or tools to write, and they are simple to write. Any text editor can be used to edit them, and the majority of operating systems come with a shell interpreter already installed.
  • Integration: More complicated automation and system administration activities are possible when shell scripts are combined with other tools and programs, including databases, web servers, and cloud services.
  • Debugging: The majority of shells come with built-in error-reporting and debugging tools that make it simple to find and quickly fix problems with shell scripts.

Starting with Bash scripting

When you log in log in to the Linux shell it looks like something :

[admin@linux ~]$

Any command that need to be ran has to be entered after the $ sign and you can see the output on the terminal.

Generally, commands follow this syntax:

command [OPTIONS] arguments

Let us see few more examples basic bash commands and see their outputs.

  • date: Displays the current date
admin@linux:~/shell-tutorial$ date
Tue Dec 26 13:08:57 IST 2023
  • pwd: Displays the present working directory.
admin@linux:~/shell-tutorial$ pwd
/home/admin/shell-tutorial
  • ls: Lists the contents of the current directory.
admin@linux:~/shell-tutorial$ ls
check_reachability.sh count_prime.sh
  • echo: Prints a string of text, or value of a variable to the terminal.
admin@linux:~/shell-tutorial$ echo "Hello bash"
Hello bash

Creation and Execution of Bash

Script naming conventions

By naming convention, bash scripts end with .sh. However, bash scripts can run perfectly fine without the sh extension.

Adding the Shebang

Bash scripts start with a shebang. Shebang is a combination of bash # and bang ! followed by the bash shell path. This is the first line of the script. Shebang tells the shell to execute it via bash shell. Shebang is simply an absolute path to the bash interpreter.

Below is an example of the shebang statement.

#!/bin/bash

You can also find your bash shell path

which bash

Creating the bash script

We will create a simple script prompting the user to enter a path. Create a file using the editor of your choice

vi run_all.sh

Add the below content to the above file

#!/bin/bash

echo "Today is " `date`
echo -e "\nenter the directory path"
read the_path
echo -e "\n The selected path has the following files and folders: "
ls $the_path

Executing the script

Provide the right permission to execute the scripts

chmod u+x run_all.sh

Here let me quickly brief you about the file permission

  • chmod modifies the ownership of a file for the current user :u.
  • +x adds the execution rights to the current user. This means that the user who is the owner can now run the script.
  • run_all.sh is the file we wish to run.

Now we can run the scripts using different way

  • sh run_all.sh
  • bash run_all.sh
  • ./run_all.sh

Now run the script :

> sh run.sh

Today is Tue Dec 26 08:02:47 IST 2023
enter the directory path
/tmp
your path has the following files and folders:
check_reachability.sh count_prime.sh

Examining the script file

Let’s examine the script more closely, line by line. I’m showing the identical script once more, but with line numbers this time.


Line 1: The path of the bash shell is indicated by the shebang (#!/bin/bash).
Line #2: The date and time are shown on the terminal by the echo command. The date is in backticks, as you can see.
Line #4: We need the user to provide a legitimate path.
Line #5: The input is read by the read command and is stored in the the_path variable.
line #8: To view the current files and folders, use the ls command and the variable containing the stored path.

Conclusion

In this chapter we have learnt what is bash and what potential benefits we can derive when we start using the scripting. We went through the terminal and understanding how the command would be executed in the Linux shell terminal. Finally we also created our first bash script and we successfully executed to get the intended result. We examined the various lines used in the script file. This blog finally comes to an end but thats not the end for the bash. In our next blog we will lean about variables, Input/Output, conditonal statements and many more essential elements of bash scripting.

Bash Project : GNU Project for Bash

Leave a comment