What is a Bash Script? – Linux Hint

Did your father ever tell you to get you a glass of water? You have the choice to say no, but computers don’t have that choice. The computers do exactly what you tell them to do. Bash is just a shell that allows you to communicate with and instruct your computer. A script is actually just a set of instructions given to the computer to perform several important tasks. The script makes it possible to automate various tasks, with the luxury of getting results faster than a normal procedure. Usually you write a simple or temporary failover command into the terminal and it is executed immediately. In bash-scripts you can give multiple instructions or commands at the same time and the computer only executes them when you execute the script. In short, a bash command can be executed in the terminal, but to execute a combination of multiple commands at the same time, you need to create a bash script.

Why is bash useful and for whom is it useful?

If you want more control over your operating system and want to perform various tasks related to the operating system, then the baston is your solution. By the way, we do not only deal with the scripting language, but also with the tools belonging to the Linux operating system. Each tool in Linux has its own task, and each one does it individually. Bash is really useful when you have to combine all these tools and connect them in such a way that they all work together harmoniously to accomplish a task that would otherwise be very difficult. For example, everything related to Linux can be done in other programming languages such as Python or Perl, but it is very difficult to perform different tasks related to the operating system. The easiest way, in black and white, to do something about Linux is to use bash. For anyone who wants to do tasks involving Linux programs (such as ls, cd, cat, touch, grep, etc.), it is really useful to learn bash instead of another programming language.

How does Bash relate to other programming languages?

When we talk about bash, we know that bash is not a general purpose programming language, but a command line interpreter. Bash is really useful for performing tasks related to different tools and processes, with the ability to combine different processes and make them work for a single purpose. Bash is very useful for editing and manipulating inputs and outputs, which is very difficult to do in other general programming languages such as Python, C and others. However, when it comes to data structures and complex tasks such as complex data manipulation, bash cannot handle such tasks and we have to turn to programming languages such as Python, Perl, C, etc. You can create software or tools in programming languages, but none of them can be built on a basha. However, you can use Bash to make the tools work, or combine them to work efficiently. It’s like building a rocket, given this metaphor, programming languages help you build a rocket, while the Bash helps you to steer the rocket and determine the direction and landing of the rocket on the moon or Mars.

How do I create and run bash scripts?

To create a bash-script, you first need to create a text file with a .sh extension at the end of the filename. You can create a bash-script file with the terminal.

After typing the above command, press Enter and a bash-script file will be created in the current working directory. But that’s not the point, we created a script file, but we need to perform certain actions to complete and execute the script. First open the script file in the Nano Editor or gedit and enter it in the first line:

This is the first default rule for any bash-script file that identifies it as a bash-script. All scripts that don’t have #!/bin/bash in the first line are not considered bash scripts, so make sure you add this line at the beginning of each script. After adding this line you can now start writing the script. For example, I would write a simple echo command here:

echo $ This is LinuxHint, the best learning platform for Bash.

After writing this command, you can now save it and return to your terminal. Write it on your terminal:

You can see script.sh in white text, indicating that the script is a non-executable file, as executable files are usually green. Then look at the left side where you will see a template of type -rw-r-r- indicating that the file is only read and written.
The first part that contains rw are the rights for the owner, probably the current user.

The second part with r is the permission for a group where we have multiple users.

During the 3. The part that contains the r is the public permission, which means that anyone can get this permission for the file in question.

r means permission to read, w means permission to write, x means permission to execute. Clearly, we can’t see the X in relation to the scenario. Ш. There are two ways to add access rights to executable files.

Method 1

With this method you can write a simple chmod command with ‘+x’ and it adds executable permissions.

However, this is not the most efficient way to give permission to turn around, because it not only gives permission to the owner, but also to the group and the public, which we absolutely do not want for safety reasons. Behold:

Method 2

This method allows you to use numbers to dictate folder access rights. Before I go into that, let me give you an idea of what these songs mean and how you can use them to manipulate rights.
read = 4
write = 2
execute = 1 1
The permission numbers would be three digits after the chmod command, each digit stands for the permissions of the owner, the group and other (public) persons. For example, you can give read, write and output rights to the owner and read rights to the group and others:

If you have noticed, you will have understood that for the owner we have added the number for reading, writing and output in the first digit such as 4+2+1=7, and for the group and others we use the number for reading, which is 4.

Running the Bash script

We’ve finally reached a point where we can run the fight script. To run your bash-script, make sure you are in the current working directory where your script is located. It’s not necessary, but it’s easy because you don’t have to write all the time. Then write ./nameofscript.sh in your terminal. In our case, the name of the script is script.sh, so we write it:

3 Simple examples of bash scripts

Hello LinuxHint
We first create a bash file in the current working directory:

The following must be included in the file

#! /bin/bash
Hello LinuxHint echo

After writing, try to write the changes to the file by pressing Ctrl+O, and if you want to save the name, type in the name, otherwise change the name and press Enter. Now press Ctrl+X to exit the Nano Editor. You will now see a file named F_script.sh in your current directory.
To run this file, you can change the permissions to make it executable, or you can write:

Echo Control
When talking about echo control, it is just used to print almost everything, as long as it is within quotation marks. If you execute an echo command without a flag, it usually leaves a line and prints the result. For example, if we have a scenario:

#! /bin/bash
echo Print in the following line

Once it’s saved, if we make it work:

When using the -n flag with echoes, it is printed on the same line.

#! /bin/bash
echo -n Print on a line

Once it’s saved, if we make it work:

If we use n or t in quotes, it will be printed as is.

#! /bin/bash
echo nPrint on the same underwear

However, when we use the -e flag, not only all this disappears, but also n and t are applied, and you can see the changes in the result below:

#! /bin/bash
echo -e nPrint on the same underwear.

A comment in BASH
A comment is a string that is not relevant to your computer. Anything you write as a comment will be cancelled or ignored by the computer and will not affect the code you write. Comments are generally considered a more useful way for a programmer to understand the logic of the code, so that they can remind him of the logic and the reasons why he wrote code in a particular way when revising certain parts of the code. The comments can also be used by other programmers who want to make changes to the code. If you have written a piece of code and you don’t want to delete it, but you want to see the result without that particular piece of code, you can comment on it and execute it. Your program will work well, will give good results as long as this extra code is still present in your script, but it is not effective because of the comments. Every time you want to use this code again, please comment on these lines and you can go.
There are two ways to write bash-style comments: one is to write comments on a single line, the other is to write comments on multiple lines.

Comments on one line
In comments on one line, we use the # character to comment on the whole line. Anything followed by a # in the string is considered a comment and has no real value when we run the script. This one-line comment can be used to convey the logic and understanding of the code to those who have access to it.

echo -e nPrint on the same wash
# This script helps us to use /n and /t

Multi-line comment
Suppose you want to comment a hundred lines in your script. In this case, it will be difficult for you to use comments on one line. You don’t want to waste time on every line. We can use: and then any comments we have. This allows you to comment on certain lines by simply typing 3 characters, which is convenient and useful.

#! /bin/bash’
: This is a script that makes
, n and t work and receives the
application so we have the required output ‘
echo -e nPrint on the same underwear.

See 30 examples of bash scripts on Linuxhint.com.

30 examples of base case scenarios

6 Main bash-script lessons

1. Quota declaration
The quota declaration is a very useful tool for decision making. It is widely used in programming languages. In most cases we have to make decisions based on certain conditions. The condition manager evaluates this condition and makes a decision. In Bash, we use a conditional operator as in any other programming language. The syntax for the use of condition operators in bash is slightly different from that of other programming languages. The if condition is the most commonly used conditional statement in bash and other general purpose programming languages. When a condition evaluates that condition and makes a decision. This condition is also called a test print. There are many options you can use while sitting in the Bash. If the condition is used with another block. If this condition is true, the operators in the block will be executed, otherwise another block will be executed. There are several ways to use the if-operator condition in Bash, which are as follows

  1. If the statement
  2. Another explanation
  3. Nested, if desired
  4. Explanation of the elixir

Operator as
Operator as evaluates only this condition if it is true, then operators or commands in the operator when it is executed, otherwise the program is terminated. In bash, if the condition starts with the keyword if and ends with the keyword fi. The keyword is used to define a block of operators or instructions that are executed when a certain condition is met. Report the variable and use the condition whether the value of the variable exceeds 10 or not. The term -gt is used to estimate more than one condition, while the term -lt is used to estimate less than one condition.

#! /bin/bash
#Condition statement as
as [ $VAR -gt 10 ]
, then
echo $VAR more than 10
#Exit as condition

http://server.digimetriq.com/wp-content/uploads/2020/10/1602773121_153_What-is-a-Bash-Script—Linux-Hint.png–Linux-Hint.png operator, if different from the operator, if different from the operator used as conditional operator. operations or commands after the state is true if the state is true. Otherwise, another block will be executed if this condition is not true. The other block is followed by the if block and starts with another keyword.

# /bin/bash
# declare condition as
if [ $VAR -gt 10 ]
echo $VAR more than 10
# declare block
echo $VAR less than 10
#end condition

The if condition can be used to evaluate various conditions. We can use both the Operator(s) and the Operator(s) to evaluate multiple conditions within one statement.

#! /bin/bash
#Condition statement as
if [[ $VAR -gt 10 && $VAR -lt 100 ]]]]
followed by
echo $VAR more than 10 and less than 100
#Declaration block
echo condition not met
#Exit if condition
is hot.

Nested as operator
Nested as operator, we have one as operator in the as operator. Firstly, in assessing the declaration, whether it is correct, and secondly, in assessing the declaration.

# !/bin/bash
#Condition statement if
if [[ $VAR -gt 10 ]]
#as condition in another if condition, if transition
if [ $VAR -gt 10 ]] is 100]
echo $VAR greater than 10 and less than 100
# Clarification of block
greater than
echo condition not met
# Output as condition
echo $VAR less than 10

Operator, if elected.
Operator, if elif. is used to evaluate multiple diseases The first condition begins with the if-block, the other conditions are followed by the keyword elif. Let’s look at the previous example of a variable number and implement the if elif operator in our bash-script. The eq-operator is used as a tie-breaker.

#!/bin/bashVAR=20# Clarify the state if [[ $VAR -eq 1 ] ] followed by echo Variable value equal to 1 elif [[ $VAR -eq 2 ] ] followed by echo Variable value equal to 2 elif [[ $VAR -eq 2 ] ] echo Variable value equal to 2 elif [[ $VAR -eq 3 ] ] The value of the variable is 2 elif [[ $VAR -gt 5 ] ] followed by echo The value of the variable is greater than 5 fi.

2. The
hinges are an integral and fundamental part of any programming language. Unlike other programming languages, Bash also uses loops to run a task repeatedly until this condition is met. The cycles are iterative, they are an excellent tool to automate these kinds of tasks. The front loop, loop, pendant and on loop is used in Bash.
Let’s discuss these hinges one by one.

While the loop
While the loop executes the same operators or commands more than once. It evaluates the condition and executes the operators or commands until the condition becomes true.
This is the basic syntax for using each cycle in Bash.

where [ test condition or expression ]
represents the instructions.

Let’s implement the while loop in our script.sh file. We have a VAR variable with a value of zero. In the while loop we have set the condition that the loop must run until the VAR value becomes less than 20. The value of the variable is increased by 1 after each iteration. In this case, the loop starts rotating until the value of the variable is less than 20.

#! /bin/bash
while [ $VAR -lt 20 ]
echo Current value of variable $VAR
#Incremes the value of 1 in VAR

http://server.digimetriq.com/wp-content/uploads/2020/10/1602773122_677_What-is-a-Bash-Script—Linux-Hint.png–Linux-Hint.png LoopThe loopcycle is the most commonly used in any programming language. Used to perform an iterative task. This is the best way to perform repetitive tasks. Define a loop for our script.sh file and use it to perform a repetitive task.

#! /bin/bash
for (( i===0; i<20; i++ ) ).
echo Hello and welcome to linuxhint
# increment variable i
Echo This is the end of the loop for

For the hinge
The other type of hinge used in Bash is the loop hinge. It also performs the same sentence several times or performs it several times. The cycle starts until the condition is calculated. until the condition becomes false. until the condition is fulfilled. until the condition is fulfilled. The syntax of the cycle is as follows:

To [state]


Let’s set up the cash flow loop in our script.sh file. One cycle until the state is false (the value of the variable is less than 20)

#! /bin/bash
to [ $VAR -gt 20 ]
echo Hello and welcome to linuxhint
#Increment variable i
echo This is the end of the loop to

3. By reading the user and writing to the
screen, Bash allows the user to enter any string or data value on the terminal. The line or data entered by the user can be read from the terminal, stored in a file and printed on the terminal. In a base file, the data entered by the user can be read with the keyword read, and we store it in a variable. The contents of a variable can be displayed on the terminal with an echo command.

#! /bin/bash
echo Write something to terminal
# Save value entered in VAR
, read VAR
echo You have entered $VAR

You can use multiple options with a single playback command. The most commonly used options are -p and -s. A note is displayed in -p and you can enter it on the same line. With the -s option, the input is in silent mode. Input symbols are displayed on the terminal. It is useful to enter some confidential information, namely passwords.

#! /bin/bash
read -p Enter E-mail : E-mail
echo Enter password
read -s Password

4. Reading and writing files
Text files are essential components for reading and writing data. The data is temporarily stored in text files and is easy to read from them. Let’s first talk about writing data to a text file and then about reading data from text files.

Writing text files
Data can be written to a file in different ways:

  • Use a hook with a right angle or larger than the (>) sign.
  • With double rectangular angle (>>)
  • With the tee shot.

The correct character in square brackets (>) for writing data
is the most common way to write data to a text file. We record the data and then use >. The > sign indicates the text file in which we should save the data. However, no files are added and the old file data is completely replaced by new data.

# user enters the name of the text file in
read -p enters the name of the file FILE
# user enters the data to be stored in the text file
read -p enters the data to be stored in the DATA file
# store the data in the text file
# > enter the file name.
echo $DATA > $FILE

The English character in square brackets (>>) for recording data
>> is used to store the output of any command in a file. For example, the ls -al command displays the content and access rights to a file in a specific directory. The output data is stored in a file.

# User enters the name of the text file in
read -p Enter the name of the file in FILE
# Save the output of the command in the file
ls -al >> $FILE

The tee command is used to write data to a text file.
The tee command in Bash is used to write the output of the command to a text file. It prints the job output on the terminal and stores it in a text file.

#! /bin/bash
# User enters the name of the text file in
read -p Enter the name of the file in FILE
# Save the output of the command to the file with tee
ls -al | tee $FILE command

By default, the tee command overwrites the data in the existing file. However, the -an option of the tee command can be used to add a file.

#! /bin/bash
# User enters the name of the text file in
read -p Enter the name of the file in FILE
# Save the output of the command in the file with the command tee
ls -al | tee -a $FILE

Read text files
The cat command is used to read data from a file. It is usually used for this purpose. It simply prints the contents of a text file on the terminal. Print the contents or data of the file using the cat command on the terminal.

#! /bin/bash
# User enters the name of the text file in
read -p Enter the name of the file in FILE
# Read the data of the text file in
cat $FILE

5. Running other programs of Bash
Bash gives the right to run other programs of the bash-script. We use the Run command to launch other programs from Bash. The execution command replaces the previous process with the current process and starts the current program. For example, we can open a nano, gedit or Vim editor from a bash-script.

#! /bin/bash
# runs the Bash
exec nano editor

#! /bin/bash
# run gedit from bash
exec gedit

In the same way, we can run the browser application from the base. Now let’s start the Mozilla Firefox browser.

#! /bin/bash
# running firefox
exec firefox

In addition, we can run any program from the base with the command execution.

6. Command line processing
Command line processing refers to the processing of data entered on the terminal. Command line data is processed for many purposes, i.e. for reading user input, interrupting commands and reading arguments. Earlier we discussed the team for the lecture. The read command is also used for processing from the command line. In this section we will look at how to deal with command line arguments. In Bash, we can deal with arguments that are passed on or written on the terminal. Arguments are treated in the same way as they are transferred. That’s why they call it a position parameter. Unlike other programming languages, the indexing of arguments in Bash starts with 1. The dollar sign ($) is used to read the arguments. For example, $1 is the first argument, $2 is the second, and so on. Arguments can be dissected for various reasons, for example to get information from the user.

# /bin/bash
echo Enter your name
# process first argument
echo First name: $1
# process second argument
echo Middle name: $2
# process third argument
echo Last name: $3
echo Full name : $1 $2 $3

Reading terminal data by means of reading and analysing arguments are the most suitable examples of command line processing.

History of fishing and comparison with other mussels

Currently, bash is the most important component of UNIX and Linux-based systems. The Bourne mould was originally designed by Stephen Bourne. The task of Stephen Bourne’s projectile was to overcome the limitations of the projectiles that already existed at that time. For the Bourne shell, UNIX introduced the Thompson shell. However, Thompson’s shell was very limited in the treatment scenario. The users could not execute enough scripts. In order to overcome all these limitations of the Thompson mussel, the Bourne mussel was introduced. It was developed in Bell’s lab. In 1989, Brian Fox revolutionized the Bourne Shell by adding many more features and called it Bourne Again Shell (BASH).

Name of the mould Year Platform Description Comparison with BASH
Thompson scale 1971 UNIX The automation of the script was limited. The user can only do a small part of the script. BASH overcomes the limitations of the Thompson shell and the user can write large scripts.
Bourne Moule 1977 UNIX This allows us to write and execute a large number of scripts. Bourne Shell does not offer a command editor or other access to DeepL. BASH offers design improvements in combination with a command editor.
POSIX casing 1992 POSIX POSIX transferable casing. It offers multiple labels and job control. BASH is popular for tasks that do not require mobility.
Z-shell 1990 UNIX Z Shell is multifunctional. It is a very powerful shell that provides functions such as automatic filling, spell check and automatic filling. BASH does not have some of the features offered by Z-Shell.


BASH is a very powerful tool that makes it possible to execute commands and scripts. The BASH script allows you to automate daily tasks and commands. A BASH script is a combination of several commands. The BASH file ends with the .sh extension. Before running the BASH script, we need to update the file permissions and set the .sh file permissions for executable files. This article explains BASH and the BASH script with simple examples and important lessons. It also describes the history of BASH and compares its capabilities with those of several other powerful grenades.

Related Tags: