How to Open a New Tab in Gnome Terminal from Command Line

How to open a new tab in GNOME Terminal from command line?

#!/bin/sh

WID=$(xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)"| awk '{print $5}')
xdotool windowfocus $WID
xdotool key ctrl+shift+t
wmctrl -i -a $WID

This will auto determine the corresponding terminal and opens the tab accordingly.

How to open a new terminal within a terminal in Windows OS with Command Prompt

It's not entirely clear what you're asking, but here's a few pointers for Windows:

  • If you're using regular console windows (conhost.exe):

    • To open another cmd.exe console window from cmd.exe (run start /? for help):

      • start cmd
      • Or even just start by itself.
    • To open another PowerShell console window from PowerShell (run help Start-Process for help; alias start works too):

      • Start-Process powershell (Windows PowerShell)
      • Start-Process pwsh (PowerShell (Core) 7+)
    • Of course, you're free to open a PowerShell window from cmd.exe and vice versa; just use the executable names shown above.

  • If you're using Windows Terminal (run wt -h for help or see the CLI documentation):

    • wt cmd / wt powershell / wt pwsh
      • Note: If you have created custom Windows Terminal profiles for your shells, use -p <profileName> instead of the executable name; e.g., wt -p myCmd instead of wt cmd, but be sure to specify the profile name exactly as defined, case-sensitively, otherwise the default profile is quietly used.
    • If you want to open a new tab in the same window (assuming you're calling from Windows Terminal):
      • wt -w 0 cmd / wt -w 0 powershell / wt -w 0 pwsh

As for the relationship between the original and the new console window:

  • While the process associated with the new window is a child process (that therefore inherits the caller's environment variables),

  • it lives on independently of its parent; that is, terminating the parent process (such as by closing the original console window) has no impact on the child process (and its window).

Run script in new tab in gnome terminal

The reason that:

gnome-terminal --tab -- '/bin/bash -c "ls";bash'

Fails is that it is looking for a program with that as its name. This is an instance of chain loading where the rest of the arguments are passed as is to exec. Those first argument is the name of the program. With this quoting it receives the entire command and options as a single program name.

Change the quoting to:

gnome-terminal --tab -- /bin/bash -c "ls;bash"

Open tabs in Windows Terminal w/ Ubuntu WSL & Zsh

zsh ls does not make sense. In general,

zsh some_file

asks zsh to interpret some_file as a text file containing a zsh script. To invoke the external command ls, you would at least have to write

zsh -c ls

In your case, it makes perhaps (depending on how you configured zsh) sense to run it as login shell. The invocation would be then something like

zsh -lc ls

Of course the zsh exits after that immediately, because you don't open an interactive shell. To achieve this, you could overlay your non-interactive zsh with an interactive zsh process after the ls has been performed:

zsh -lc "ls; zsh"

Programmatically open tab in gnome-terminal, execute command, and have tab stay open

Gnome-terminal can either execute a command or open a shell, but not both.

There is a workaround to do both by encapsulating the command and subsequent invocation of the shell into one command.

$ gnome-terminal -e "bash -c \"echo foo; echo bar; exec bash\""

For more alternatives read my answer to a similar question on stack overflow: https://stackoverflow.com/questions/3512055/avoid-gnome-terminal-close-after-script-execution/3531426#3531426

Bash: Script Calling Gnome Terminal Leads To Child Process Error?

-x and --command are incorrect/obsolete options, try this :

sudo gnome-terminal --window --wait --tab --active --geometry=120X60 \
--title="$1" --working-directory="$code_directory" -- "$file_path"

To keep the terminal window open :

sudo gnome-terminal --window --wait --tab --active --geometry=120X60 \
--title="$1" --working-directory="$code_directory"\
-- bash -c "$file_path; exec bash"


Related Topics



Leave a reply



Submit