The “Bourne again shell” command language processor is also known as Bash. The series of commands in a Bash script contains the plain text of the series of commands that are performed while the script is being run. The script must begin with “#!/bin/bash” in Bash. Several approaches for running a Bash script under Linux are used in this article.
How to Run a Bash Script
Bash is the GNU operating system’s shell or powerful scripting translator. With the use of Bash scripting, a user can neatly group the commands and examine them to prevent a potential damage to data, folders, and data structures. Your scripts’ coding conventions make it easier for other programmers to read the code and help in debugging. Although Bash scripting is not a language of programming, it is different which allows you to optimize the various tasks and work on administrative duties as well. Since other programming languages make it difficult to accomplish these tasks, Bash scripting is often preferred over other programming languages when using multiple tools at once. This is because the Bash script is extremely quick in these types of situations.
Let’s begin the process of opening the Bash script. To achieve this, we must first build a Bash script. Therefore, we create a Bash file on the desktop. Consequently, in the first step, we first open the terminal and change the directory to the desktop using the “cd” command. Then, we type “Desktop” and end it with a slash (“/”).
Now that we entered this command, we’re automatically taken to the desktop directory. To see how many files are now present on the desktop and see their names, we execute the “ls” command which shows all of the desktop’s files on the terminal window. We currently have four files on our desktop with the names “file.txt”, “name.txt”, “output.txt”, and “record.txt” as you can see in the following image. All of these files are text files and we need to open a Bash file so we first create a Bash script.
Creating a Bash File
Before constructing a Bash script, we demonstrate a few different Bash shells. To do this, type “cat” on the terminal, followed by the slash (“/”) and the “etc” term. We then write the slash (“/”) and the “shells” at the end.
As you can see in the following image, all the available shells are displayed in the terminal window when we type this command by pressing the enter key on the keyboard. The “/bin/sh”, “/bin/bash”, “/usr/bin/bash”, “/bin/rbash”, “/usr/bin/rbash”, “/bin/dash”, and “/usr/bin/dash” make up the total number of them, which is seven. Here, “/bin/bash” is the shell that we use. However, “/usr/bin/bash” is also an option.
Therefore, the “which bash” command now provides the path to the Bash shell when entered.
Now, when we enter this command, it provides the path which is “/usr/bin/bash” as seen in the following:
Now, in the subsequent section, we use the “touch” command to construct a Bash script. The main purpose of the “touch” command is to change the timeframe. Despite not being its main purpose, the tool frequently performs the file creation. The update and accessibility times for any specific file can be changed using the terminal software. The touch function only creates a file if one is not present. Thus, after typing “touch”, a blank space is provided before writing the name of the Bash script that we want to use. In this case, the Bash script’s name is “bashfile.sh” because it is a Bash script. So, the “.sh” extension is used.
As you can see in the following image, a file with the “bashfile.sh” name is created on the desktop when we run this command.
The text file named “bashfile.sh” is currently visible.
Adding a Bash Script
To make it a Bash script, we must follow a few steps. To do this, open the file from the desktop and type “#” followed by the exclamation mark “!” and a space. Then, type “/bin/bash”. This tells your system that the file is now a Bash script. Now that we have “echo”, we can input any information or element that we want to put in as a statement. Then, we use the inverted commas. Inside of these inverted commas, we type the statement for Bash which is “Linux is a family of open source Unix operating system”. We now save it by clicking the save option which is located at the top right.
To check the execution of the Bash script, we use the “ls” command in the following step:
When the command is performed, as shown in the following image, the Bash file is not executed because it is displayed in white color. In that case, the file is not yet executed. However, when it is displayed in green color, it indicates that the file is now executed.
There are two ways to modify the permission. However, in this case, we use the “chmod” command to make it executable in the following step. The “chmod” command is utilized to modify a file or directory’s access permissions. Its acronym is “change mode”. It cannot alter a symbolic link’s permissions. Even the symbolic links that we encounter during recursive directory traversal are disregarded. So, after typing “chmod”, we type “+x” to make the file executable before writing the “bashfile.sh” filename so there won’t be any errors when we run this command.
The “ls” command is now entered in the following step to check the file’s execution:
As you can see in the following image, after running this command, the file name “bashfile.sh” changed to green, indicating that it can now be executed.
Now, in the subsequent section, we run the Bash script. To do this, we use the command in which we type “./” followed by the name of the “bashfile.sh” Bash script.
Run the Bash Script
Linux@linux:~/Desktop$ ./bashfile.sh
As you can see in the following image, when we run this command, the Bash script runs on the terminal screen which displays the “Linux is a family of open source Unix operating system” statement. We can see the results of the statement which we echoed here.
Using SH to Run a Bash Script
In this part, we run the Bash script on the terminal using “sh”. The “sh” is a system software task interpreter for Linux and Unix-like platforms. Some built-in commands are offered by sh. A subset of sh is Bash. Therefore, we use the command in which we write “sh” and then a space, followed by the name of the bash script which is “bashfile.sh”.
You can see that the Bash script is now successfully running when we input this command.
Using the Bash Keyword to Run a Bash Script
To run the Bash script on the terminal in this section, we utilize the “bash” keyword in the command. To do this, we enter “bash” followed by a space, then the script’s name which is “bashfile.sh”.
You can see that when we execute this command, it displays the statement we echoed which is “Linux is a family of open-source Unix operating systems”.
Conclusion
We discussed the topic on how to run a Bash script in Linux in this article. In the provided examples, we examined the alternate methodologies to run the Bash scripts. In the first stage, we discussed how to create a Bash file on the desktop and how to make it executable using the “chmod” command. In the second section, we used the “bash” and “sh” keywords to run the Bash script.