Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

1. Overview

Running shell scripts in Linux is a common task that administrators and developers perform regularly. Sometimes, there’s a situation where we might want to execute a script in a new terminal window while continuing to use the current terminal. This is particularly useful for monitoring scripts and handling multiple tasks simultaneously.

Consequently, Linux provides several ways to run scripts in a new terminal depending on the desktop environment and terminal emulator being used.

In this tutorial, we’ll explore various methods to run a shell script in a new terminal window from the current terminal session.

2. What Is a Shell Script?

A shell script is a text file containing a series of commands that the shell interpreter executes in sequence. Therefore, shell scripts can automate repetitive tasks, execute commands in a defined order, and help in complex system administration tasks.

3. Benefits of Running Scripts in a New Terminal

Running a script in a new terminal has several advantages:

  • separation of workspaces: this keeps script execution separate from the current terminal, avoiding interference with ongoing tasks
  • real-time monitoring: allows real-time monitoring of the script’s output without interfering with our main terminal tasks
  • error tracking: if a script fails or encounters an error, it’s easier to debug when running it in a dedicated terminal window
  • persistent output: by using suitable command options, we can keep the new terminal open, enabling us to see the final output even after the script finishes execution

4. Writing a Simple Shell Script

To begin with, let’s write a basic shell script that displays a simple message. First, we’ll create a new file named hello.sh using the nano text editor:

$ sudo nano hello.sh

Then, we’ll add the following lines to the hello.sh file:

#!/bin/bash
echo "hello world"

Furthermore, let’s save the file and make it executable using chmod along with the +x option:

$ sudo chmod +x hello.sh

Now that the script is executable, let’s run it:

$ ./hello.sh
hello world

Subsequently, the output displays hello world confirming that the script worked as intended.

5. Using gnome-terminal

gnome-terminal is a terminal emulator for the GNOME desktop environment, providing users with a command-line interface to interact with the underlying operating system.

In case we don’t have gnome-terminal installed, we’ll need to update the indexes of available packages and install gnome-terminal using apt-get on Debian-based systems:

$ sudo apt-get update
$ sudo apt-get install gnome-terminal

Now, we can use the gnome-terminal command to open a new terminal window while executing the hello.sh script:

$ gnome-terminal -- bash -c "./hello.sh; exec bash"

Let’s understand what the different parts of the command mean:

  • : separates the options for the terminal from the commands we want to run
  • bash -c: runs the quoted string as a command in the new terminal
  • ./hello.sh: runs the script
  • exec bash: keeps the terminal open after the script finishes executing

6. Using konsole

Another method to run a shell script in a new terminal from the current terminal is using konsole. konsole is the default terminal emulator for the KDE desktop environment, offering a powerful and customizable interface for command-line interactions.

Similar to gnome-terminal, let’s use konsole to launch a new terminal window and run the shell script:

$ konsole --hold -e bash -c "./hello.sh"

Here’s a brief explanation of each option in the konsole command:

  • –hold: keeps the terminal open after the script finishes executing
  • -e: specifies the command to execute
  • bash -c: runs the quoted string as a command in the new terminal
  • “./hello.sh”: runs the script inside the new konsole window

7. Using xterm

xterm is a terminal emulator for the X Window System, which provides a basic command-line interface on Unix-like operating systems, including Linux. While it may not offer the advanced features of more modern terminal emulators like gnome-terminal or konsole, it’s highly reliable and lightweight, making it suitable for systems where minimal resource usage is desired.

To illustrate, let’s use xterm along with -hold -e, which keeps the terminal open after executing the hello.sh script:

$ xterm -hold -e "./hello.sh"

In this case, the command opens a new xterm window, executes the script, and keeps the terminal open to view the output.

8. Using alacritty

alacritty is a fast, GPU-accelerated terminal emulator known for its simplicity and performance. Additionally, alacritty is lightweight, cross-platform, and doesn’t come with advanced features like tabs or split windows, making it ideal for users who prioritize speed and efficiency in a terminal.

Likewise, we can use alacritty along with -e bash -c “./hello.sh; exec bash” which executes the script in a new terminal. In particular, exec bash keeps the terminal open afterwards:

$ alacritty -e bash -c "./hello.sh; exec bash"

The command launches an alacritty window and ensures the new terminal stays open.

9. Conclusion

In this article, we explored different methods to run a shell script in a new terminal from the current terminal in Linux. Running shell scripts in a new terminal is a straightforward process in Linux, offering various methods tailored to different desktop environments.

Therefore, by utilizing terminal emulators like gnome-terminal, konsole, xterm, and alacritty, users can manage script execution and monitor outputs without disrupting the current terminal tasks.