this may be a bit obscure but I'm trying to do something a bit tricky :
I'm writing a script that opens up a new tab with commands, I'd like to switch back to first tab after the user has input to the secondary tab (the fact that this tab focuses is intended behavior and the most important) :
echo "new tab is about to open..."
gnome-terminal --tab --active --title="install dependencies..." -- bash -c 'echo "hello";
#other commands;
read user_input;
#now I want to switch back to first tab while this tab works in the backgroud;
command;
command;
command;
command;
command;
command;
command;'
echo "hello welcome back to the correct tab, we have been waiting for you! :)"
read new_user_input;
is such a thing possible?
I had this idea because clearly the --active argument calls something that brings the new tab into focus (and even works if the tab calling this is not active). so something of the nature must be possible.
Manual Method
As per this Q&A: Is there a hotkey to switch between tabs in the default terminal app?
After opening the new tab you can return to the previous tab with Ctrl + pg up
Automated Method within script
In order to send the signal to Bash Shell (gnome-terminal) use
xdotool
:In your script issue this command:
I've just finished installing and testing on Ubuntu 16.04 LTS and it works perfectly. It should work on all X11 desktops.
More conventional method
OP desires Wayland support and more importantly a POSIX method that works with many *NIX distributions.
From this Q&A:
... comes this command:
We will modify the example command to look like this:
This is what
second-script.sh
looks like:Always remember to marks scripts executable using:
Note: This opens a second terminal window that stays open until user closes it. This can be changed to auto-close.
Our first (Parent) script will look like this:
Advantages of current approach:
second-script.sh
) completes output is still visible until window is closed.Wayland tools
There aren't many Wayland tools to control active windows.
I did find this: ydotool but you'll need to compile it by hand. There is no Debian/Ubuntu installation package. There is an Arch Linux installation package which some of your users can use.
Well, it consumes more time then I expected. Finally, I manage to get what you want. Try this script in this I get the window name with the use of 'xdotool' and switch to the active window using 'wmctrl'.
You can try simulating keystrokes to change to your other tab:
You can use
xdotool
to simulate any keystroke combination. To change tab in gnome-terminal useCtrl + Page_Up
(go one tab back). Soxdotool key --clearmodifiers Ctrl+Page_Up
will change to your old tab.To make sure that the focus of this keystroke is the terminal I used
wmctrl
which can be used like this:wmctrl -a "install dependencies"
.So a command to switch back to your previous tab would be:
You can install those tools using
sudo apt-get install wmctrl xdotool
Setting the title of terminal windows is removed from gnome-terminal. So I tried this on the rox terminal. You need to install this two software:-
I can manage to get what you want. Try this script for testing.