0

For an application, I need to open a new terminal window and later execute some commands in that.

I tried the command

gnome-terminal

And it works properly, it open a new terminal, but when i want to send commands i cannot, it says that failed parsing arguments, so I'm not sure about how should i do it

gnome-terminal --ls
# Failed to parse arguments: Unknown option --ls
2
  • what do you mean with "send some commands"? Do you mean "start it with a command", or to you intend to first start the terminal emulator and then later execute different commands in it? Commented Jan 10, 2023 at 14:49
  • the 2nd option I want to open a terminal, and later execute the commands in it Commented Jan 10, 2023 at 14:51

2 Answers 2

3

You can do something like this:

gnome-terminal -- bash -c "ls; exec bash"

Please note, you have to have a long-running application in the new terminal for it to stay open long enough for human eyes. It can be a brand new bash or just a long running application:

gnome-terminal -- top
gnome-terminal -- tail -f somefile.txt

Other terminals sometimes have a built-in option to stay open:

xterm -hold -e ls

But you can do the same trick with exec:

xterm -e "ls; exec bash"

Also you can, of course, add a -hold option:

xterm -hold -e "ls; exec bash"

But in this case, the xterm window will stay open even after the bash exits. And you would have to close window by yourself.

0

This requires a two step process.

  1. you need to start gnome-terminal running a program that waits for commands to be "sent in"
  2. you need to speak to said program to make it execute the things you want it to execute.

The right syntax to start gnome-terminal with a command to execute is

gnome-terminal -- command

not gnome-terminal --command as you did.

Having sorted out how to do 1., we need to find a program that does serves as a daemon that will listen to commands being sent in.

tmux is such a server. You can run

gnome-terminal -- tmux -L 'a unique name for a socket'

to start your gnome-terminal with an empty shell inside.

You can then use tmux' CONTROL MODE to send commands to that tmux server, e.g. to attach to the session currently displayed in the gnome-terminal, then make a new frame in that, running your command of choice in that frame. See man tmux for more detail.

Honestly, though:

gnome-terminal is an interactive terminal emulator. You just seem to want to display some output in a graphical manner; you don't seem to expect gnome-terminal to get input from the user. The right thing to do in that case is simply not use gnome-terminal, but use whatever you're planning to use to send commands to display a window with a constant-width font text field, and print whatever output you want there.

1
  • Thank you Marcus, as you said one of my biggest problem was the syntax. And after your good explanation i found that this is not the right thing that i want to do it, thanks. Commented Jan 11, 2023 at 9:58

You must log in to answer this question.

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