1. Overview

We often need to run bash commands or scripts while working in a Graphical User Interface. Therefore, the text terminal’s capabilities are necessary.

We’ll examine ways to effectively run bash commands using gnome-terminal‘s tabs.

2. What Is gnome-terminal?

gnome-terminal is an emulator of a text terminal. We can start it with the bash 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 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. In detail, 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., xterm, a 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 bash commands inside using the ‘–‘ separator.

The double hyphen ‘–‘ denotes the end of options to gnome-terminal. Everything after this mark is regarded as bash commands.

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

gnome-terminal -- sleep 10s

Some sleeping is necessary because the terminal closes immediately after the command inside is done.

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"

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 during closing the terminal.

6. Running Commands in Tabs With a Script

Let’s start some routine tasks in separate tabs with the script ‘launcher’:

#!/bin/bash

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"

We’re going to collect all tabs in the same terminal. Therefore, we need to use a temporary one to start our script.

So, let’s create it with a –window switch:

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

As a result, the script sends off 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 Tabs With xdotool

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

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

This bash script adds exactly one tab to a gnome-terminal, where it is called from:

#!/bin/sh

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

Let’s examine the xdotool’s 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, wmctrl -a make the window active by switching to the appropriate workspace, raising, and focusing it.

It is necessary because the script works in an interactive graphical environment. Consequently, the users may change the active window when the script’s running.

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

8. Running Command With xdotool

Let’s modify the previous example slightly to allow starting a script in the tab. Basically, we’ll use the type command of xdotool, which simulates the user’s typing:

#!/bin/bash

WID=$(xdotool getactivewindow)
xdotool windowfocus $WID
xdotool key ctrl+shift+t
wmctrl -i -a $WID
sleep 1; xdotool type --clearmodifiers "[email protected]"; xdotool key Return;

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

Assuming that our script is called xlauncher, we’ll start the pwd command, which is provided as an argument [email protected]:

$ ./xlauncher pwd

9. Conclusion

In this article, we used gnome-terminal to facilitate work with bash. First, we created tabs with the gnome-terminal command.

Then, we learned how to start scripts inside produces in this ways tabs.

Finally, we showed how to achieve the same goal with the xdotool based user simulator.

Comments are closed on this article!