Execute Scripts in Linux Shell [7 Methods Inside!]

run bash script

Run Bash script is a fundamental skill for anyone using Linux or Unix-like systems. 

Bash scripts automate tasks, making complex procedures simple and repeatable. System administrators rely on these scripts to take care of a long list of system management and operational tasks. 

After you have written or created the script, the next critical step is to run Bash script. This is an important step that determines the impact of the script on system performance. 

In this detailed tutorial, we will discuss several methods you can use to run Bash scripts in the terminal. We will start with the simplest and then move on to more complex examples. 

Table Of Contents

  1. 7 Methods of Running Bash Scripts in Linux
    1. The Prerequisites
    2. Method #1: Run Bash Script Using sh
    3. Method #2: Directly Run Bash Script using bash
    4. Method #3: Run Bash Script using source
    5. Method #4: Run Bash Script by Specifying the Path
    6. Method #5: Pass Arguments to the Bash Scripts
    7. Method #6: Run Bash Script using the GUI
    8. Method #7: Run Bash Script from Anywhere
  2. Conclusion
  3. FAQs

7 Methods of Running Bash Scripts in Linux

Let’s start with the prerequisites.

The Prerequisites

Before running a Bash script, ensure you have the following:

  • A system running a mainstream Linux distribution
  • A user account with root or sudo privileges
  • Execute permissions for your scripts 

Permission to run a Bash script is a critical requirement for smooth system operations. If you need to brush up on these ideas, we recommend our tutorial on viewing file permissions and changing these file permissions to allow you to execute Bash scripts without any issues. 

Let’s now divide into the methods you can apply to run a Bash script.

Method #1: Run Bash Script Using sh

sh, also known as Bourne Shell, is a popular standardized scripting language available on almost all UNIX-like systems.

The sh command is primarily used to run scripts intended for the Bourne shell. However, it can also execute Bash scripts in certain cases where Bash-specific features are not included in the script. 

Consider the following example where we have a script named example_script.sh with the following content:

#!/bin/bash

echo "Hello, World!"

Now, change file permission to 755 to make the script executable.

To run this script using sh, open your terminal and type:

# sh example_script.sh 

The output of the test script would be as follows:

Hello, World!

Run Bash Script Using sh

Note: Using sh to run a Bash script can be useful if you’re on a system where Bash is not the default shell or need to ensure compatibility with the Bourne shell.

Method #2: Directly Run Bash Script using bash

Running a script with bash is straightforward and ensures that you can leverage all Bash-specific features in your scripts.

We will continue the example_script.sh we used in the previous section. Launch the terminal and run this script using bash by running the following command:

# bash example_script.sh

This will execute the script and display the output:

Hello, World!

Directly Run Bash Script using bash

We recommend this method for scripts that utilize features exclusive to Bash, as it guarantees the script will behave as expected.

Method #3: Run Bash Script using source

Sourcing a script (using source or .) executes the script within the current shell session, allowing it to modify the environment of the running shell.

For instance, consider the following example_script.sh that sets an environment variable MY_VAR:

#!/bin/bash 

export MY_VAR="Hello, World!"

Once you have created the script, you can source it in the current shell session by running either of the following two commands:

# source example_script.sh

or

# . example_script.sh

After sourcing this script, echo MY_VAR in the terminal:

# echo $MY_VAR 

Hello, World!

Run Bash Script using source

Sourcing is particularly useful for scripts that set environment variables, define functions, or modify the shell environment in other ways.

Method #4: Run Bash Script by Specifying the Path

Often, the script you wish to execute is not in the current working directory. In this case, you can run a Bash script by specifying its full path.

For instance, consider the situation where you have a script named myscript.sh located in /home/user/scripts. You can run this script by typing the full path (/home/user/scripts/my_script.sh) in the terminal. 

However, before that, you should ensure the script has execute permission. For this, run the following command:

# chmod +x /home/user/scripts/my_script.sh

Run Bash Script by Specifying the Path

Current Directory Execution

If your script is located in the current working directory, you can simply execute it with period (.) option that tells the system to execute the script located in the current directory:

# ./my_script.sh 

Current Directory Execution

Method #5: Pass Arguments to the Bash Scripts

You can pass arguments to Bash scripts to alter their default behavior or provide necessary data for processing.

For instance, consider a script greet.sh that takes a name as an argument and greets the user. In this script, $1 represents the first argument passed to the script.

#!/bin/bash 

echo "Hello, $1!"

If required, you should change the execute permission with the following command:

# chmod 755 greet.sh

Now, you can run this script and provide a name as the argument:

# ./greet.sh John 

The output will be as follows:

Hello, John!

Pass Arguments to the Bash Scripts

Method #6: Run Bash Script using the GUI

If you prefer working with the GUI instead of the terminal, you can use the available file managers to execute the scripts.

Consider the following script, named my_script.sh, that is located in the Desktop directory. Follow these steps to execute the script:

  1. Navigate to the Desktop directory in the file manager.
  2. Right-click on the my_script.sh script.
  3. Select Run in Terminal or Execute (the exact option might vary).

This action opens a terminal window and executes the script, showing any output or prompts in the terminal.

Method #7: Run Bash Script from Anywhere

If you wish to run a script from any location, we recommend adding its full path to the system’s PATH variable.

Consider the following scenario where a script named my_script.sh is located in the /home/user/scripts/ directory. You can run this script from anywhere by adding the full path to the PATH variable:

# export PATH=$PATH:/home/user/scripts

Now, you can simply type my_script.sh in the terminal, regardless of your current directory, to run the script.

Run Bash Script from Anywhere

Make Scripts Globally Executable

Ensure the script is executable and the PATH change is persistent by adding the export line to your .bashrc or .profile file.

Now that you know several ways of executing Bash scripts, we recommend our other tutorials on how to use case statements in Bash scripts, add if-else statements, use string comparisons, print variables with the printf command, and write effective comments in Bash scripts.  

Conclusion

Understanding various methods to run a Bash script enhances your ability to manage and automate tasks in a Linux environment. Each method serves different scenarios and requirements, from using sh or bash to sourcing scripts, specifying paths, handling arguments, leveraging the GUI, and executing scripts globally.

RedSwitches bare metal hosting provides a reliable and efficient solution for those managing extensive scripts or requiring robust system performance. 

At RedSwitches, we’re dedicated to helping you discover the perfect server solutions to drive your business to new heights. So, if you’re looking for a robust server, we offer the best dedicated server pricing and deliver instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.

FAQs

Q. What is a shell script?

A shell script is a program written in a scripting language that is interpreted by the shell of an operating system, such as Linux. It allows users to automate tasks and execute commands on the command line.

Q. How do I create a Bash script?

To create a Bash script, you need to write a series of commands in a plain text file with a .sh extension. You can use any text editor to create the script file.

Q. What is the shebang line in a bash script?

The shebang line, which starts with #!, specifies the path to the Bash interpreter that will be used to run the script. It is placed at the beginning of the script file.

Q. How do I run a Bash script using the command line?

To run a Bash script on the command line, you need to make the script executable using the chmod command and then execute it using the ./ syntax followed by the script name.

Q. How can I schedule scripts to run at specific times?

You can use tools like cron on Linux to schedule the execution of scripts at specific times. By setting up a cron job, you can automate the running of scripts at intervals you define.

Q. What are some basic Bash scripting commands?

Some basic Bash scripting commands include echo for printing messages, read for accepting user input, and if-else statements for creating conditional execution in scripts.

Q. How can I get started with Bash scripting as a beginner?

To get started with Bash scripting, you can follow an easy-to-follow tutorial that covers the basics of shell scripting, syntax, creating scripts, and executing commands on the command line.