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?

2
  • See man gnome-terminal again. It's there.
    – user535733
    Nov 9, 2017 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. Dec 9, 2020 at 16:41

4 Answers 4

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:

8
  • 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 9, 2017 at 17:41
  • @J.R. Here is a detailed explanation: askubuntu.com/a/967720/566421
    – pa4080
    Nov 9, 2017 at 17:53
  • 1
    Hi man I solved it myself by setting export PROMPT_COMMAND="exit"
    – J.R.
    Nov 10, 2017 at 8: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, 2019 at 9:58
  • 1
    use konsole -e ... in Kubuntu
    – alchemy
    Apr 15, 2020 at 16:08
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.

5
  • 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 9, 2017 at 17:27
  • @J.R. the command is executed but the terminal closes after it executes the command
    – lapisdecor
    Nov 9, 2017 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 9, 2017 at 17:36
  • try gnome-terminal -e "bash -c ls;bash"
    – lapisdecor
    Nov 9, 2017 at 17:47
  • Update: gnome-terminal says -e may become deprecated, and instead use gnome-terminal -- commandhere
    – Andrew
    Jun 4, 2021 at 12:14
1

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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .