1. Overview

We often need to run shell commands or scripts while working in a graphical user interface (GUI). Therefore, even then, we might need a text terminal’s capabilities. In fact, we can even manage a terminal from within the command-line interface (CLI).

In this tutorial, we’ll examine ways to run shell and terminal commands using gnome-terminal tabs effectively.

2. What Is gnome-terminal?

gnome-terminal is an emulator of a text terminal. We can usually start it with the command gnome-terminal.

We’re going to use version 3.40.3 throughout this tutorial.

In some Linux distributions, gnome-terminal isn’t available out of the box, so we may need to install it manually.

3. Creating Tabs in gnome-terminal

With the –tab option, the gnome-terminal command creates tabs. So, let’s obtain two tabs:

$ gnome-terminal --tab --tab

The tabs appear in the last created gnome-terminal. Further, if we issue this command in the existing gnome-terminal, new tabs attach to it.

On the other hand, if we start from a different kind of terminal, e.g., xterma brand new gnome-terminal pops up with exactly two tabs.

4. Running Commands in gnome-terminal

Let’s start gnome-terminal and run a sequence of commands within it using the separator.

In particular, the double hyphen denotes the end of options to gnome-terminal. Everything after this mark is regarded as shell commands.

So, let’s run sleep inside a newly created terminal:

$ gnome-terminal -- sleep 10s

We do this since the terminal closes immediately after the command inside is done executing.

5. Keeping the gnome-terminal Open

Now, let’s try to keep the terminal open.

We can’t tell the gnome-terminal to do that, so we need to use Bash commands to do the trick:

$ gnome-terminal -- bash -c "sleep 2s; echo foo; exec bash -i"

Here, we use bash twice. For the first time, with option -c, just to run a sequence of commands in a new shell.

Subsequently, the nested bash -i opens an interactive shell for us. As a result, the terminal stays open.

We start the second bash through the exec command to reuse the existing shell.

Amongst others, it eliminates the warning about a process running in the terminal, which otherwise shows up when closing the terminal.

6. Running Commands in Tabs With a Script

Now, let’s make a script that starts some routine tasks in separate tabs:

$ cat launcher.sh

gnome-terminal --tab -- bash -c "sleep 1s; echo \"Foobar\"; exec bash -i"
gnome-terminal --tab -- bash -c "sleep 1s; echo \"Bar\"; exec bash -i"
gnome-terminal --tab -- bash -c "sleep 1s; echo \"Foo\"; exec bash -i"
$ chmod +x launcher.sh

The plan is to gather all tabs in the same terminal. Therefore, we use a temporary one to start our script.

So, let’s create the initial window with the –window switch:

$ gnome-terminal --window -- bash -c "./launcher.sh"

As a result, the script opens three tabs. After that, the script is done, and the temporary tab closes.

However, the dispatched interactive shells remain open after scheduled tasks are finished.

7. Creating an Open With Entry to Launch Scripts

We can also use the file manager’s Open With feature to launch scripts using gnome-terminal.

To achieve this, let’s create a file called Terminal.desktop in the ~/.local/share/applications/ directory:

$ cat ~/.local/share/applications/Terminal.desktop
[Desktop Entry]
Name=Gnome Terminal
Exec=gnome-terminal -- bash -- %f

The script above adds Gnome Terminal to the list of preferred applications for opening shell scripts.

We can now right-click on a script and select the Open With Other Application button:

Open with GNOME Terminal option

Gnome Terminal is now available in the list of applications that we can use to launch shell scripts.

8. Creating Tabs With xdotool

The xdotool utility simulates user activities such as keyboard or mouse inputs in the X11 environment. So, let’s mimic user actions during tab creation.

In interactive mode, we can create tabs using the Ctrl-Shift-T combination.

Let’s demonstrate via a Bash script that adds exactly one tab to the gnome-terminal from which it’s called:

$ cat gtaddtab.sh

WID=$(xdotool getactivewindow)
xdotool windowfocus $WID
xdotool key ctrl+shift+t
wmctrl -i -a $WID

Let’s examine the xdotool commands used in the script:

  • getactivewindow returns the identifier of the active window – the current gnome-terminal in our case
  • windowfocus sets the focus to the window identified by WID
  • key simulates the ctrl+shift+t sequence to open a new tab

In addition, the wmctrl command makes the window active by switching to the appropriate workspace, raising, and focusing it. This is usually necessary because the script works in an interactive graphical environment. Consequently, users may change the active window when the script is running.

Both xdotool (version 3.20160805.1) and wmctrl (version 1.07) aren’t standard commands, so we might need to install them.

9. Running Command With xdotool

Let’s modify the previous example slightly to enable starting a script in the tab.

For that, we use the type command of xdotool, which can simulate a user typing:

$ cat gtaddtabrun.sh

WID=$(xdotool getactivewindow)
xdotool windowfocus $WID
xdotool key ctrl+shift+t
wmctrl -i -a $WID
sleep 1; xdotool type --clearmodifiers "$@"; xdotool key Return;
$ chmod +x gtaddtabrun.sh

The –clearmodifiers option assures that the simulated typing is not affected by, e.g., pressing the CapsLock button.

Assuming that our script is called gtaddtabrun.sh, we’ll start the pwd command, which is provided as an argument:

$ ./gtaddtabrun.sh pwd

This runs the pwd command within gnome-terminal.

10. Conclusion

In this article, we used gnome-terminal to facilitate work with the shell and saw ways to control gnome-terminal from the CLI.

First, we created tabs with the gnome-terminal command. Then, we learned how to use bash commands to keep the gnome-terminal open.

Finally, we explored how to achieve the same goal using xdotool.