How to Write Bash Scripts?

To write a Bash Script we will follow the steps –

  • First, we will create a file with the .sh extension.
  • Next, we will write down the bash scripts within it
  • After that, we will provide execution permission to it.

To create and write a file with the .sh extension we can use gedit text editor. The command for it will be –

gedit scriptname.sh

The first line of our script file will be –

#!/bin/bash

This will tell, the system to use Bash for execution. Then we can write our own scripts.

Let’s write down just a simple script that will print some lines in the terminal. The code for it will be –

#!/bin/bash         

echo "Hello, w3wiki"

Now we will save the file and provide the execution permission to it. To do so use the following command –

chmod +x scriptname.sh

Next to execute the following script we will use the following command –

./scriptname.sh

Here is the terminal shell pictorial depiction after executing the above commands as follows:  

 

Here the script file name is gfg.sh.

Now we can also write more complicated commands using Bash Scripts. Here is an example below in which we are using a condition statement –

Example Script:

#!/bin/bash         
Age=17
if [ "$Age" -ge 18 ]; then
    echo "You can vote"
else
    echo "You cannot vote"    
fi

Output:

You cannot vote

Here is the terminal shell pictorial depiction after executing the above script as follows:  

 

In the above way, we can execute multiple Bash commands all at once.

Now Let’s look into the other important concepts related to Bash Scripting.

Bash Scripting – Introduction to Bash and Bash Scripting

Bash is a command-line interpreter or Unix Shell and it is widely used in GNU/Linux Operating System.  It is written by Brian Jhan Fox. It is used as a default login shell for most Linux distributions. Scripting is used to automate the execution of the tasks so that humans do not need to perform them individually. Bash scripting is a great way to automate different types of tasks in a system. Developers can avoid doing repetitive tasks using bash scripting. 

Bash scripting supports variables, conditional statements, and loops just like programming languages. Below are some of the applications of Bash Scripts – 

Table of Content

  • Applications of Bash Scripts:
  • Advantages of Bash Scripts:
  • How to Write Bash Scripts?
  • File Names and Permissions
  • Variables
  • Input and Output
  • Functions
  • Decision Making
  • String and Numeric Comparisons

Similar Reads

Applications of Bash Scripts:

Manipulating files Executing routine tasks like Backup operation Automation...

Advantages of Bash Scripts:

It is simple. It helps to avoid doing repetitive tasks Easy to use Frequently performed tasks can be automated A sequence of commands can be run as a single command....

How to Write Bash Scripts?

To write a Bash Script we will follow the steps –...

File Names and Permissions

In the above example, we have saved the file using gfg.sh name and also provided execute permission using chmod command. Now, let’s understand why we have done that....

Variables

We can use variables in bash scripting. Below is a sample program to understand the usage of variables in Bash scripts....

Input and Output

Input & output are fundamental concepts for shell scripting. A script can take one or more inputs and can also produce zero or many outputs. It may even produce some errors.  Let’s understand this with an example –...

Functions

In programming, A function is a block of code that performs some tasks and it can be called multiple times for performing tasks. The simplest example of the use of function in Bash scripting can be given as –...

Decision Making

In programming, Decision Making is one of the important concepts. The programmer provides one or more conditions for the execution of a block of code. If the conditions are satisfied then those block of codes only gets executed....

String and Numeric Comparisons

The string comparison means in the shell scripting we can take decisions by doing comparisons within strings as well. Here is a descriptive table with all the operators –...

Conclusion

In this article we discuss Bash scripting which plays a vital role in automating tasks within the Linux environment. As a command-line interpreter, Bash offers simplicity and ease of use for developers to create scripts that streamline routine operations, manipulate files, and execute complex tasks. While Bash scripts can enhance productivity by avoiding repetitive actions and executing sequences of commands efficiently, they also come with considerations such as managing permissions, handling variables, and implementing decision-making structures. By mastering Bash scripting, users can harness its power to optimize their workflow and enhance system administration tasks on Linux platforms....