Command to open new tab in the current terminal and pass command to run on new tab
Solution 1
GNOME Terminal
offers no generic command line solution to open a new tab in an already existing window.
(Why? Because it may have multiple windows open, and it wouldn't know in which one to open the new tab. You might say you only have one window, but if such a feature was implemented, the software would need to be consistent and need to do something reasonable when it had multiple windows, which it couldn't really do.)
Hacks, such as simulating the user pressing a key, exist as per an already filed answer, but work only under X.Org, not under Wayland.
Beginning with forthcoming version 3.28, the command gnome-terminal --tab
will open a new tab in the GNOME Terminal window where this command is executed from, if this command is executed from within GNOME Terminal.
(How does it know in which window to open the new tab in this case? It knows by setting a certain environment variable upfront to a different value in each of the terminals it opens. That environment variable lets it locate the desired window.)
So for your particular case, you could do:
gnome-terminal --tab -- bash -c 'cd /; exec bash'
This way you get a running shell in the new tab once the cd /
is completed. If you just did gnome-terminal --tab -- some command
, then the tab will close immediately after some command
exits. By using bash -c 'some command; exec bash'
instead, the tab will have a running bash
process once some command
completes.
Solution 2
You must simulate pressing shift + ctrl + T in the terminal. Programs exist to doing that:
If you want to do it from the command line
For example you can use xdotool
- a program that lets you simulate keyboard input.
sudo apt-get install xdotool
then type in the terminal:
xdotool key ctrl+shift+t
That will simulate pressing the key combination.
Jamie Hutber
Updated on September 18, 2022Comments
-
Jamie Hutber over 1 year
I know that
gnome-terminal
will open a new window, I wondered is there a way to open a new tab, not just a new window?After this, is there a way to pass a command to the new window for example
gnome-terminal && cd /
Which will result in
- New Terminal Opens
- New Terminal then runs
cd /
[Edit] Will these answers will say how to pass cmds to another terminal, it does not answer how to open a new TAB. Which is what I am after, I do not want more than one terminal open.
-
derHugo over 6 yearsPossible duplicate of Passing multiple commands to gnome-terminal from a script or maybe look also here
-
Jamie Hutber over 6 yearsBut then how do I pass a command to the newly opened window? Thanks btw
-
Mahdi over 6 yearsIf it's possible you can add the command to your
~/.bashrc
file. Every time terminal opens this file is invoked.