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.
Last updated: October 18, 2024
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.
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.
Running a script in a new terminal has several advantages:
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.
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:
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:
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.
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.
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.