2

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?

  • See man gnome-terminal again. It's there. – user535733 Nov 9 '17 at 16:49
5

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.


Update: The -x/-e syntax is now deprecated - Ubuntu 18.04. The new recommended way 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/; ..."

More examples of usage of this format:

  • 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 '17 at 17:41
  • @J.R. Here is a detailed explanation: askubuntu.com/a/967720/566421 – pa4080 Nov 9 '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 9 '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 7:28
  • 1
    Hi man I solved it myself by setting export PROMPT_COMMAND="exit" – J.R. Nov 10 '17 at 8:04
1

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

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

0

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 9 '17 at 17:27
  • @J.R. the command is executed but the terminal closes after it executes the command – lapisdecor Nov 9 '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 9 '17 at 17:36
  • try gnome-terminal -e "bash -c ls;bash" – lapisdecor Nov 9 '17 at 17:47
0

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

xterm -hold -e cmd

The hold option keeps the window open.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.