How To Run Bash Script In Linux? - GeeksforGeeks (2024)

Last Updated : 13 Feb, 2024

Summarize

Comments

Improve

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.

Table of Content

  • The Shebang Line:
  • Using bash or sh to Run Bash Script in Linux
  • Using Source to Run Bash Script in Linux
  • Making the Script Executable with chmod to Run Bash Script in Linux
  • How To Run Bash Script In Linux – FAQs

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.

How To Run Bash Script In Linux? - GeeksforGeeks (1)

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

How To Run Bash Script In Linux? - GeeksforGeeks (2)

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.

How To Run Bash Script In Linux? - GeeksforGeeks (3)

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

How To Run Bash Script In Linux? - GeeksforGeeks (4)

using chmod and executing the script.

How To Run Bash Script In Linux? - GeeksforGeeks (5)

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.



M

meetgor

How To Run Bash Script In Linux? - GeeksforGeeks (6)

Improve

Next Article

How to Create a Shell Script in linux

Please Login to comment...

How To Run Bash Script In Linux? - GeeksforGeeks (2024)
Top Articles
USCIS Announces New FY 2024 Funding Opportunity Under the Citizenship and Integration Grant Program
The Ten Most Beautiful Streets in the World - Falstaff
English Bulldog Puppies For Sale Under 1000 In Florida
Toyota Campers For Sale Craigslist
Phone Number For Walmart Automotive Department
PRISMA Technik 7-10 Baden-Württemberg
Mama's Kitchen Waynesboro Tennessee
Craigslist Free Stuff Appleton Wisconsin
Jennette Mccurdy And Joe Tmz Photos
Arrests reported by Yuba County Sheriff
Moviesda Dubbed Tamil Movies
Obituary | Shawn Alexander | Russell Funeral Home, Inc.
Skylar Vox Bra Size
Readyset Ochsner.org
Meritas Health Patient Portal
Dexter Gomovies
Log in or sign up to view
Richland Ecampus
Keurig Refillable Pods Walmart
Pokemon Unbound Shiny Stone Location
Wbiw Weather Watchers
The BEST Soft and Chewy Sugar Cookie Recipe
Low Tide In Twilight Ch 52
Defending The Broken Isles
Netwerk van %naam%, analyse van %nb_relaties% relaties
Discord Nuker Bot Invite
Violent Night Showtimes Near Johnstown Movieplex
Divide Fusion Stretch Hoodie Daunenjacke für Herren | oliv
When His Eyes Opened Chapter 3123
Stephanie Bowe Downey Ca
Emuaid Max First Aid Ointment 2 Ounce Fake Review Analysis
Dl.high Stakes Sweeps Download
Napa Autocare Locator
Slv Fed Routing Number
Gerber Federal Credit
Hannibal Mo Craigslist Pets
Gets Less Antsy Crossword Clue
Evil Dead Rise (2023) | Film, Trailer, Kritik
The Largest Banks - ​​How to Transfer Money With Only Card Number and CVV (2024)
Ucsc Sip 2023 College Confidential
Pa Legion Baseball
Lucifer Morningstar Wiki
Citroen | Skąd pobrać program do lexia diagbox?
About Us
15 Best Places to Visit in the Northeast During Summer
Xre 00251
855-539-4712
Every Type of Sentinel in the Marvel Universe
Prologistix Ein Number
211475039
Shad Base Elevator
Latest Posts
Article information

Author: Corie Satterfield

Last Updated:

Views: 5586

Rating: 4.1 / 5 (42 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Corie Satterfield

Birthday: 1992-08-19

Address: 850 Benjamin Bridge, Dickinsonchester, CO 68572-0542

Phone: +26813599986666

Job: Sales Manager

Hobby: Table tennis, Soapmaking, Flower arranging, amateur radio, Rock climbing, scrapbook, Horseback riding

Introduction: My name is Corie Satterfield, I am a fancy, perfect, spotless, quaint, fantastic, funny, lucky person who loves writing and wants to share my knowledge and understanding with you.