how can I open a extra console and run a program in it with one command?

43,224

Solution 1

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:

Solution 2

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

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

Solution 3

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

Share:
43,224

Related videos on Youtube

J.R.
Author by

J.R.

Updated on September 18, 2022

Comments

  • J.R.
    J.R. over 1 year

    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?

  • J.R.
    J.R. over 6 years
    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.
  • lapisdecor
    lapisdecor over 6 years
    @J.R. the command is executed but the terminal closes after it executes the command
  • J.R.
    J.R. over 6 years
    @lapisdecor ah, ok, so I should add something like "cmd -k" for genome as well?But why gnome-terminal -e ./main not working?
  • lapisdecor
    lapisdecor over 6 years
    try gnome-terminal -e "bash -c ls;bash"
  • Andrew
    Andrew almost 3 years
    Update: gnome-terminal says -e may become deprecated, and instead use gnome-terminal -- commandhere