1. Introduction

In this tutorial, we’ll explore a few approaches to run commands in separate terminals. We’ll specifically focus on terminal emulators rather than pure CLI-based TTYs, pseudo-terminals, and terminal multiplexers.

First, we’ll look at the Konsole terminal emulator, a popular choice for KDE-based environments. Next, we’ll cover the xterm command, a terminal emulator found in many Linux distributions. Lastly, we’ll discuss the GNOME Terminal, the default terminal emulator for systems using the GNOME desktop environment, such as Ubuntu and Fedora.

2. Using Konsole

To open different terminals and run a command in each one, we’ll first use konsole within a KDE environment. konsole launches the Konsole terminal emulator. We’ll use konsole in a shell script to open separate terminals.

2.1. Install konsole

The konsole command may not be available by default. We’ll install it using the apt package manager:

$ sudo apt update && sudo apt install konsole

In addition, we can install binutils if we get an error message when starting konsole:

konsole: error while loading shared libraries: libQt5Core.so.5: cannot open shared object file: No such file or directory

Let’s use apt to install the binutils package:

$ sudo apt update && sudo apt install binutils

Then, we’ll configure /usr/lib/x86_64-linux-gnu/libQt5Core.so.5 via strip:

$ sudo strip --remove-section=.note.ABI-tag /usr/lib/x86_64-linux-gnu/libQt5Core.so.5

This should correct any issues that usually occur while running the konsole command from a Linux machine or Linux WSL (Windows Subsystem for Linux).

2.2. Run Multiple Terminals

For instance, if we want to open four terminals and run the echo command with different arguments, we can create a test.sh file and use konsole:

$ cat test.sh
#!/usr/bin/env bash
konsole --noclose -e echo Hello World. Running this from terminal 1! &
konsole --noclose -e echo Hello World. Running this from terminal 2! &
konsole --noclose -e echo Hello World. Running this from terminal 3! &
konsole --noclose -e echo Hello World. Running this from terminal 4! &

In this script, we’ve used the –noclose option to stop the terminal from closing automatically once it executes a command. In addition, the -e option ensures that the code runs in the terminal window.

Next, we’ve typed the command we want to execute which is echo. Lastly, we’ve used & to run each task in the background in a different terminal.

Once we run the script, we’ll get the output of each echo command in different terminals:

multiple terminals using konsole

Notably, the window ordering depends on our GUI.

2.3. Run Multiple Interactive Terminals

Furthermore, we can interact with newly opened terminals by launching /bin/bash using konsole:

$ cat test.sh
#!/usr/bin/env bash
konsole --noclose -e /bin/bash &
konsole --noclose -e /bin/bash &
konsole --noclose -e /bin/bash &
konsole --noclose -e /bin/bash &

This opens four interactive terminals.

In addition, we can display the output using echo as well as interact with the terminals using &&:

$ cat test.sh
#!/usr/bin/env bash
konsole --noclose -e echo Hello World. Running this from terminal 1! && /bin/bash &
konsole --noclose -e echo Hello World. Running this from terminal 2! && /bin/bash &

The script above executes /bin/bash only if the first command executes without any error. In this way, we can execute as many commands as we want to.

2.4. Add Multiple Tabs

Alternatively, we can use konsole to open several different tabs and execute commands in those terminals:

$ cat test.sh
#!/usr/bin/env bash
konsole --noclose --new-tab -e echo Hello World. Running this from terminal 1! &
konsole --noclose --new-tab -e echo Hello World. Running this from terminal 2! &
konsole --noclose --new-tab -e echo Hello World. Running this from terminal 3! &
konsole --noclose --new-tab -e echo Hello World. Running this from terminal 4! &

Notably, we’ve also added the –new-tab option with konsole along with –noclose and -e options. In addition, we must ensure that the konsole version is above 20.12 for the –new-tab option to work:

multiple terminal tabs using konsole

We can check the version using the –version option:

$ konsole --version

Finally, the –new-tab option won’t work in a WSL environment as it isn’t equipped with the necessary integration and support for managing the Konsole emulator tabs.

3. Using xterm

Another method to open multiple terminals uses xterm. This command launches the Xterm emulator, a lightweight emulator available for UNIX operating systems.

xterm is known for its lightweight and simple interface. Furthermore, it utilizes minimal hardware resources which results in faster startup time and lower memory consumption as compared to the Konsole emulator.

3.1. Install xterm

Although prevalent, similar to konsole, xterm isn’t always present on Linux machines by default. Therefore, we’ll install it first:

$ sudo apt update && sudo apt install xterm

Once the installation is complete, we can check its version using the –version option:

$ xterm --version

Now, let’s use xterm to open some terminals.

3.2. Run Multiple Terminals

For this example, we’ll introduce a loop instead of writing the command multiple times:

$ cat test.sh
#!usr/bin/env bash
echo "Enter the value of n:"
read n
for ((i = 0; i < n; i++ ))
do
  xterm -hold -e echo “Hello world: $i” && /bin/bash &
done

In this script, we run the xterm command n times. We also use the -hold and -e options with xterm. In this case, -hold keeps the terminal open even when the code has completed its execution. Furthermore, after -e we’ve added two commands that we want to execute in the Xterm emulator.

Through this approach, we can run the same command in all the terminals. However, if we want to execute a variety of commands, then we can avoid a loop in the script. Alternatively, we can create an array of commands.

4. Using a Distribution-Based Terminal

Apart from xterm and konsole, we can also use gnome-terminal on Linux machines that use the GNOME desktop environment.

To use the gnome-terminal to open multiple terminals, we’ll create a test.sh script:

$ cat test.sh
#!/bin/bash
# Commands to run in each terminal
commands=(
  "echo 'Hello from Terminal 1'"
  "ls -l"
  "date"
  "ping -c 3 google.com"
)
# Open a new terminal window for each command
for ((i=0; i<${#commands[@]}; i++))
do
  gnome-terminal --tab --title="Terminal $((i+1))" -- bash -c "${commands[i]}; exec bash"
done

We’ve modified the approach in this script. Instead of writing commands separately like konsole and writing the same command like xterm, we’ve created an array of commands.

After that, we used a for loop. The stopping condition of the loop is the length of the array. Furthermore, we’ve used gnome-terminal to open various tabs using the –tab option.

Next, we’ve assigned a title to each terminal using the –title option. The is used as a delimiter and indicates the end of the gnome-terminal and the beginning of the commands to be executed within the terminal.

In addition, we invoke the bash shell. Further, the -c option retrieves the commands array and executes it. Lastly, exec bash ensures that the Bash terminal stays open even after the code has been executed. This allows us to continue interacting with the terminal. Here’s the preview:

multiple terminals using gnome-terminal

If we want to open different terminal windows instead of tabs, we can use the –window option instead:

gnome-terminal --window --title="Terminal $((i+1))" -- bash -c "${commands[i]}; exec bash"

With this modification, the commands execute in different terminal windows with distinct titles.

5. Conclusion

In this article, we’ve seen different emulators to open terminals and run commands in each terminal. First, we went over Konsole for KDE. After that, we saw how Xterm can meet our requirements. Finally, we saw that gnome-terminal is a convenient option for GNOME desktop environment users.