Open In App

How To Run Bash Script In Linux?

Improve
Improve
Like Article
Like
Save
Share
Report

Bash scripts, also known as shell scripts, are powerful tools in the world of command-line automation. They allow you to perform a series of tasks or execute commands by writing scripts that can be run in a terminal or command-line interface. However, the question often arises: how do you run a Bash script in Linux? In this article, we will explore various methods for executing Bash scripts and provide valuable insights on best practices.

The Shebang Line:

Before we delve into the methods of running Bash scripts, it’s important to understand the shebang line. The shebang line, which is the first line in your script, indicates the interpreter that should be used to execute the script. For Bash scripts, this line should typically look like this:

#!/bin/bash

This line informs the system that the script should be interpreted using the Bash shell. Even though some methods may work without this line, it’s a good practice to include it in your script to ensure compatibility.

Using bash or sh to Run Bash Script in Linux

This is the most standard way of executing the bash script. You must have git bash installed if you are using Windows. For Linux and macOS, bash is installed by default. In this method, we type bash followed by the file name with extension i.e. sh in this case. In a terminal, run the following code by replacing the filename with your bash script filename.

bash filename.sh

Here, bash is a program that contains the shell environments necessary to run the script from the bash shell. So this will execute the script from the bash interpreter. 

Using the bash command to run the script.

We can also use sh to run the script as it will direct to the default shell in the setup environment. 

sh filename.sh

Using the sh command to run the bash script.

From the above example, we were able to run a bash script using bash as well as the sh command. If you are not in the same folder/directory as the script, make sure you specify the relative path to the script.

Using Source to Run Bash Script in Linux

This method is quite easy to run a bash script, and all of them are quite simple. We just need to type in “source” before the file/script name with an extension. In a terminal, run the following code by replacing the filename with your bash script filename.

source filename.sh

The script will simply get executed after “sourcing” the file. The source command will execute the shell script as the default bash command provided you are in the bash shell.  You need to be in the bash shell to execute the script using the source command.

Using Source to run a bash script

From the screenshot of the script running, we can see that the source works exactly like the bash or sh command. The above script is very basic, but that doesn’t matter as long as the script is errorless and bug-free. Also, you need to add the relative path here as well if you are not in the same directory as the bash script.

Making the Script Executable with chmod to Run Bash Script in Linux

This method allows you to run a Bash script as an executable, which means you can run it from anywhere in your environment as long as you have a Bash shell available. To make the script executable, you need to adjust its file permissions using the chmod command.

First, navigate to the directory where your script is located. Then, run the following command to change the file’s mode, making it executable:

chmod +x filename.sh  

The `+x` flag indicates that the file should be executable. If you are using Linux and are not the root user, you may need to use `sudo` before the `chmod` the command for permission.

After you’ve granted execution permission, you can run the script with the following command, assuming you are in the same directory as the script:

./filename.sh

If you are not on the same path as the bash script, make sure you provide the relative path to the file or the bash script.

./pathToTheFile.sh

using chmod and executing the script.

Executing a script from a relative path.

How To Run Bash Script In Linux – FAQs

How do I run a Bash script in Linux?

To run a Bash script in Linux, you first need to make sure the script has executable permissions. You can do this by using the `chmod +x` command followed by the script name. Once the script is executable, you can run it by typing `./` followed by the script name in the terminal.

Can I run a Bash script without using the terminal?

Yes, you can run a Bash script without using the terminal by double-clicking on the script file if your desktop environment supports it. You may need to set the script’s executable permissions first. Alternatively, you can run a Bash script indirectly from another program or script.

How do I run a Bash script as a specific user in Linux?

To run a Bash script as a specific user in Linux, you can use the `sudo -u` command followed by the username and the path to the script. This allows you to execute the script with the permissions of the specified user.

Why am I getting a “permission denied” error when trying to run a Bash script?

The “permission denied” error occurs when the script does not have executable permissions. You can resolve this issue by using the `chmod +x` command to grant executable permissions to the script. Alternatively, ensure that you are trying to execute the script from a directory where you have proper permissions.

Can I run a Bash script in the background?

Yes, you can run a Bash script in the background by appending an ampersand (`&`) at the end of the command when running the script. This allows the script to run asynchronously, and you can continue using the terminal while the script executes in the background. Additionally, you can use tools like `nohup` to run scripts detached from the terminal session.

Conclusion:

In this article, we’ve discussed various methods for running Bash scripts in Linux and provided best practices to ensure a smooth execution. By including a shebang line in your scripts and choosing the appropriate method for your needs, you can harness the full power of Bash scripts to automate tasks and streamline your command-line workflow. Whether you prefer using bash, sh, or source, or making your scripts executable with chmod, these methods empower you to run Bash scripts effectively and efficiently in a Linux environment.



Last Updated : 13 Feb, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads