1. Overview

In this article, we will be looking at how to run multiple bash scripts in our terminal. There might be times when we may need to run different scripts on the same terminal window. For example, it may be for monitoring or complex automation. Anytime we find ourselves in this situation, we might not know the right way to go about it. That is what we will be handling in this article.

2. Methods for Running Multiple Bash Scripts in the Terminal

There are several methods used to achieve this, but we will take a look at a few examples.

2.1. The Operators &&, ||, and ;

The first logical operator we will be looking at will be the AND operator: &&. In Bash, it is used to chain commands together. It can also be used to run two different scripts together. Let’s see an example:

$ ./script1.sh && ./script2.sh
Hello World!!
Hello World!!

We have two Bash scripts, namely script1.sh and script2.sh. These scripts only return “Hello World” upon running them.

If the first script we are executing encounters an error, the second script doesn’t run.

The second logical operator we will be looking at will be the OR operator ||. It can be used to run different commands together. The OR operator runs two commands one after the other, but the second command only gets executed when the first command finishes with an error.

With our two scripts, we can run them both like so:

$ ./script1.sh || ./script2.sh 
Hello World!!

The second script gets executed only when the first script encounters an error.

The last operator we will be looking at will be ;. It can also be used to run two scripts sequentially. Unlike the OR operator, the second script still gets executed when our first script ends properly.

In this example we will try to use an invalid command to display Hello World in our terminal

$ ./script1.sh ; ./script2.sh 
./script1.sh: 1: ech: not found
Hello World 2!!

There are more ways to group commands, such as using curly braces or parentheses.

2.2. The bg and fg Commands

A handy Linux command for running two scripts simultaneously would be the bg command. Using the bg command, we can resume and control how jobs are run in our terminal. We can set a script to run in the background while running a new script in the foreground. Let’s take a look:

$ ./script1.sh
[1]  + 29680 suspended  ./script1.sh
$ bg
[1]  + 29680 continued  ./script1.sh
$ ./script2.sh
Hello World!!

We have modified the scripts here a little to help better our understanding of how bg works. Now, script1.sh outputs a command to sleep for 30 seconds, “sleep 30s”. While script2.sh still outputs “Hello World!!”.

When we run script1.sh, we usually would wait for the sleep command to end, but we can suspend that task and make it run in the background by suspending it with Ctrl+z followed by bg. Now we have script1.sh running in the background, and room to run any other script, in this case, it will be script2.sh to output “Hello World!!”.

If you ever decide to want to bring up the script running in the background to the foreground, you can do so by using the fg command:

$ ./script1.sh 
[1]  + 320642 suspended  ./script1.sh
$ ./script2.sh
Hello World 2!!
$ fg 
[1]  + 320642 continued  ./script1.sh

script1.sh has been brought back to the foreground with the fg command. Jobs have different states and can be sent to the background, resume and kill via commands.

2.3. Using a Script to Run Multiple Scripts

In Bash scripting, we can have a script that executes two or more scripts. To do this, add those scripts to the main script we want to run. For example, we have script3 execute the code in script1 and script2. script3.sh will have to contain this:


Now, when we run script3.sh we have:

$ ./script3.sh
Hello World!!
Hello World!!

3. Conclusion

In this tutorial, we have discussed some of the various methods we can run scripts in a terminal window. There are different methods and approaches used to achieve the goal. With this, we can now run scripts sequentially in our terminal.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.