Writing Bash Script

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

Bash scripting is a powerful tool that allows you to automate tasks, streamline processes, and enhance your productivity as a developer or system administrator. Understanding the fundamentals of writing Bash scripts is essential for anyone seeking to harness the full potential of the command line interface. This article will explore the basics of writing Bash scripts, covering important concepts such as the Bash symbol, shebang, script commands, and chaining multiple commands.

Key Takeaways

  • Getting familiar with the process of writing Bash script.
  • Getting familiar with the utilities of Bash script.

Download File

Step-by-Step Guide to Write a Bash Script

While programmers write Bash script, they follow some steps sequentially. Here, I have discussed these steps briefly.

1. Getting Started: Initiating a Bash Script

To begin writing a Bash script, open a text editor and create a new file with the “.sh” extension. This extension is commonly used to indicate that the file contains a Bash script. At the start of your script, it is important to include the “shebang” line.

2. The Shebang Line: #!/bin/bash

The shebang line, also known as the shebang or the interpreter directive, informs the system which interpreter should be used to execute the script. In the case of the Bash script, the shebang line is “#!/bin/bash”. This line should always be the first line of your script.

3. Bash Script Commands

Bash script allows you to execute commands and perform various operations. Examples of commonly used Bash Script commands are echo, read, if, else, and elif. There are some special symbols for chaining commands which help the programmer execute multiple commands in a single line.

Some Practical Examples of Writing a Bash Script

In this section, I have demonstrated some sample Bash scripts that will help you to understand the basic concept of a Bash script.

Example 01: Printing a Greetings on the Terminal

Here I will develop a Bash script that will print a greeting on the terminal. To do so, follow the below procedures.

Steps to Follow >

❶ At first, launch an Ubuntu Terminal.

❷ Write the following command to open a file in Nano:

nano sample.sh
EXPLANATION
  • nano: Opens the nano text editor.
  • sample.sh: Bash script name.

❸ Copy the script mentioned below:

#!/bin/bash

echo"Happy Scripting" #prints the greetings on the terminal
EXPLANATION

The #!/bin/bash interprets that it is a Bash script. Then the echo command prints greetings on the terminal.

❹ Press CTRL+O and ENTER to save the file; CTRL+X to exit.

❻ Run the script by the following command:

bash sample.sh
EXPLANATION
  • bash: Executes the bash scripts.
  • sample.sh: Bash script name.

A Bash script written which prints a greetings on the terminal.The above image shows that Bash script has printed a greeting text on the terminal.

Example 02: Taking Input From the User Then Print It

You can take input from the user while running a Bash script and then print it on the terminal. Here I have developed a Bash script that takes input from the user and then prints it on the terminal. To do this task, follow the below script.

You can follow the steps of example 01, to save & make the script executable.

Script (bash.sh) >

#!/bin/bash

#takeing input from users
read -p "What is your name?: " name

#printing on the terminal
echo "Welcome $name!"
EXPLANATION

At first, the Bash script asks for input from the user and keeps it on the name variable. Then the echo command prints a line on the terminal which contain the value of name variable.

Execute the following command into the terminal to make the Bash script executable.

chmod +x bash.sh
EXPLANATION
  • chmod: Change the permission of a file.
  • +x: Gives execution permission to all.
  • bash.sh: Bash script name.

Run the script by the following command:

./bash.sh

A Bash script is written in such a way that it takes a input from the user then print a message including the input value on the terminal.In the above image, you can see that the Bash script has taken input from the user and then printed it on the terminal.

Example 03: Performing Arithmetic Operations by Taking Two Input Numbers from the User

Programmers can do arithmetic operations on the Bash script. Here, I have developed a Bash script that takes two inputs from the user and then does an arithmetic summation. Afterward, print the summation on the terminal. To do so, follow the below script.

You can follow the steps of example 01, to save & make the script executable.

Script (arith.sh) >

#!/bin/bash

#taking number as input from user
read -p "Enter First Number: " a
read -p "Enter Second Number: " b

#calculation summation
var=$((a+b))

#printing the summation value
echo "Summation of The Two Number:$var"
EXPLANATION

At first, the Bash script has taken numbers a and b as input. Then I did the arithmetic summation. And finally, print the summation on the terminal.

Run the script by the following command:

./arith.sh

User inputs two numbers, then arithmetic summation is done.The above image shows that the Bash script has taken input from the user then done an arithmetic summation.

Example 04: Using a Function in the Bash Script

You can easily use a function in the Bash script. Here, I have developed a Bash script where the function is used to print a message. To do so, follow the below script.

You can follow the steps of example 01, to save & make the script executable.

Script (bash_fun.sh) >

#!/bin/bash

# A sample bash function
sample_function () {
echo "Good Morning!"
}

# Invoking the sample_function
sample_function
EXPLANATION

At first, a function named sample_function has been defined. In side the function the echo command prints a message. In the next part of the Bash script, sample_function has been called by simply putting the function name.

Run the script by the following command:

./bash_fun.sh

A function is written inside a Bash script which printed a message on the terminal.The above image shows that the Bash script has printed a message using a function.

Conclusion

Writing Bash script empowers you to automate repetitive tasks, manage complex operations, and boost your efficiency as a developer or system administrator. By understanding the fundamentals of Bash scripting, including the Bash symbol, shebang, script commands, and chaining multiple commands, you can create powerful scripts to streamline your workflow. Embrace the power of Bash scripting, and unlock new possibilities in the command line realm.

People Also Ask

How do I write a bash script in shell?
To produce a bash script, start the file with #!/bin/bash. Run ./scriptname with any options to run the script from the current directory. The shell locates the #!/path/to/interpreter when it runs a script.

What is bash used for?
Tasks like code compilation, source code debugging, change management, and software testing may all be automated with Bash. On workplace networks, network engineers use Bash to test, configure, and improve network performance.

Is Bash faster than Python?
A bash shell script starts up in 2.8 milliseconds, whereas PPython takes 11.1 milliseconds. Like Python, Bash is a general-purpose language, although both have advantages and disadvantages. Since the majority of Linux distributions use the Bash shell as the default terminal, it will always operate more quickly.

Do programmers use bash?
In order to deal with files and apps, save time, or make the most of restricted computer resources, developers on macOS and Linux PCs utilize Bash every day.

Related Articles


<< Go Back to Bash Scripting Basics | Bash Scripting Tutorial

3.7/5 - (7 votes)
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

Leave a Comment