25

So I know if I type gnome-terminal or xterm, a new window will be popped out. Then I checked the man page for these two, nothing relevant found.

Then I noticed under Mac you can do it with the program open. But it seems under Linux it's not that trivial.

Does anyone have experience?

pa4080
  • 29,831
J.R.
  • 373
  • See man gnome-terminal again. It's there. – user535733 Nov 09 '17 at 16:49
  • To get only what are interested from man command or some-command --help flag, use grep in pipe. I'd try man gnome-terminal | grep run or man gnome-terminal | grep command what will filter the output. – Enrique René Dec 09 '20 at 16:41

4 Answers4

39

Update: The new recommended syntax is:

gnome-terminal -- bash -c "<my command or script>; exec bash"
  • If you want to reach the users home directory within the above command use the environment variable $HOME: bash -c "cd $HOME/; ..."

If you look at man gnome-terminal (and gnome-terminal --help) the options -x and -e are available (and it is not explicitly written they are deprecated) but all examples there are given by the syntax provided above.


I would prefer to use the option -x that provides more reliable work than -e:

gnome-terminal -x bash -c "<my command or script>; exec bash"
  • The option -x means --execute - the remainder of the command line inside the terminal.

  • And our command is bash -c "<commands>". That means we execute a new bash shell, which should run some -c "<commands>".

  • We have two separated (by semicolon ; == new line) <commands>.

  • The first command <my command or script> will execute that we want.

  • The second command exec bash has a meaning - remain open the current gnome-terminal window. There are another possible approaches to do that. In the current case the command exec will replace the current process image with a new process image - in other words it will 'kill' the current process and will execute a new (bash) under the current PID.


More examples of usage of this format:

pa4080
  • 29,831
  • I got it. If I simply type gnome-terminal -x ./main it won't work. Cause "bash" is the parameter I have to pass to run ./main? – J.R. Nov 09 '17 at 17:41
  • @J.R. Here is a detailed explanation: https://askubuntu.com/a/967720/566421 – pa4080 Nov 09 '17 at 17:53
  • @J.R. I think gnome-terminal -x ./main shall work, but the terminal is closed too fast. Also using of /full/path/ is better than ./, you also could provide and working directory for the new gnome-terminal instance --working-directory='/home/<user>/..... – pa4080 Nov 09 '17 at 18:19
  • nice. Thnx for the help. Still one small question. Can I set it to something like the console in a programming IDE? Like the console shows "type anything to continue", then if I type enter the console will be closed rather than typing "exit". – J.R. Nov 10 '17 at 07:28
  • 1
    Hi man I solved it myself by setting export PROMPT_COMMAND="exit" – J.R. Nov 10 '17 at 08:04
  • 4
    Excellent answer! Here's how to make it shell agnostic, starting ZSH or another shell if the user has set that as default:

    gnome-terminal -- $SHELL -c "echo 123; exec $SHELL"

    – Teodor Dec 12 '19 at 09:58
  • 1
    use konsole -e ... in Kubuntu – alchemy Apr 15 '20 at 16:08
  • Giving "error constucting proxy for org.gnome.Terminal The connection is closed" in rhel 8,2 – Vinod Feb 16 '24 at 11:53
2

Another approach that will keep the window open is to use xterm:

xterm -hold -e cmd

The hold option keeps the window open.

1

gnome-terminal -e cmd will open a terminal window and run cmd within it.

  • Hi. This is also the weird part with me. After I type "gnome-terminal -e ./main", an extra console is opened but I got a error and the program is not executed correctly. If I run it with built in program like "gnome-terminal -e ls", it seems somethings happened, but no extra console will be opened. – J.R. Nov 09 '17 at 17:27
  • @J.R. the command is executed but the terminal closes after it executes the command – lapisdecor Nov 09 '17 at 17:31
  • @lapisdecor ah, ok, so I should add something like "cmd -k" for genome as well?But why gnome-terminal -e ./main not working? – J.R. Nov 09 '17 at 17:36
  • try gnome-terminal -e "bash -c ls;bash" – lapisdecor Nov 09 '17 at 17:47
  • Update: gnome-terminal says -e may become deprecated, and instead use gnome-terminal -- commandhere – Andrew Jun 04 '21 at 12:14
1

You can simply do CTRLALTT and you will open a new terminal.

Try gnome-terminal -e "bash -c command;bash"

lapisdecor
  • 1,627