在另一个(新的)终端窗口上运行命令。

如何在另一个终端窗口中运行任何命令?
示例:我打开了一个终端窗口,如果我运行像apropos editor这样的命令,它会在那个窗口上运行并输出结果。但是我想在另一个终端窗口(新窗口)上运行相同的命令,而不是在当前窗口中从第一个终端运行。
进一步澄清: 我需要一个suggest-command <command>,它可以打开一个新的终端窗口,并在其中运行指定的<command>(其中是命令建议的示例)。
如何实现这一点?

1右键点击终端图标,然后点击“打开新终端”。 - Raphael
抱歉如果我听起来像个外行,但这是我从你的问题中理解到的。 - Raphael
gnome-terminal,是吧? - Braiam
虽然打开一个新的终端可能能解决你的问题,但你也可以考虑使用nohup命令,重定向输出,并将编辑器置于后台运行。nohup apropos editor &> /dev/null & - Panther
我也不明白:为什么不开一个新的终端? - Josef Klimuk
1因为它可能需要由cron作业或其他机器人程序完成。 - DragonLord
8个回答

这可能是你要运行的内容:
gnome-terminal -- sh -c "bash -c \"!!; exec bash\""

在旧版本中,使用了-e和-x。
gnome-terminal -e "bash -c \"!!; exec bash\""
# or
gnome-terminal -x sh -c "!!; bash"

它打开gnome-terminal并执行你最后的命令(!!),并且保持打开状态,将命令输出显示在shell中,即使是像topless这样的交互式命令...

在你的情况下是:

gnome-terminal -- sh -c "bash -c \"apropos editor; exec bash\""

或者在旧版本中:
gnome-terminal -c "bash -c \"apropos editor; exec bash\""
# or
gnome-terminal -x sh -c "apropos editor; bash"

2一个稍微简短一点的命令是:gnome-terminal -x sh -c "!!; bash" - TuKsn
1Option “-x” is deprecated and might be removed in a later version of gnome-terminal - Klesun
在Xubuntu下,终端命令是xfce4-terminal,请参阅其文档以获取更多详细信息:https://docs.xfce.org/apps/xfce4-terminal/command-line - darckcrystale
更简单的方法是:gnome-terminal -- bash -c "YOURCOMMAND ; exec bash" - undefined

每个终端都是一个可以像其他程序一样启动的程序,使用&将其放在后台运行,并提供参数列表等功能。
选择要使用的终端首先取决于你所使用的系统的可用性(是否已安装),然后是它们各自的特点,最后是你个人的喜好。
  konsole   --hold -e "ls" &  
  xterm      -hold -e "ls" &  
  gnome-terminal   -e "ls" & ...  

请注意 xterm-holdkonsole--hold 之间的区别。
每个实现都有不同的选项,您需要通过帮助来进行检查。 甚至连帮助的调用方式也可能不同。 您会发现 man konsole 不起作用,因此您需要直接向可执行文件提出 --help 的请求。
这是一个您可以在系统上搜索的终端列表。
aterm          - AfterStep terminal with transparency support
gnome-terminal - default terminal for GNOME
guake          - A dropdown terminal for GNOME
konsole        - default terminal for KDE
Kuake          - a dropdown terminal for KDE
mrxvt          - Multi-tabbed rxvt clone
rxvt           - for the X Window System (and, in the form of a Cygwin port, 
                 for Windows) 
rxvt-unicode   - rxvt clone with unicode support
xfce4-terminal - default terminal for Xfce desktop 
                 environment with dropdown support
Terminator     - is a GPL terminal emulator. It is available on
                 Microsoft Windows, Mac OS X, Linux and other Unix X11 systems.
Terminology    - enhanced terminal supportive of multimedia 
                 and text manipulation for X11 and Linux framebuffer
tilda          - A drop down terminal
wterm          - It is a fork of rxvt, designed to be lightweight, but still
                 full of features
xterm          - default terminal for the X Window System
Yakuake        - (Yet Another Kuake), a dropdown terminal for KDE

最全面的答案。 - liakoyras

启动您想要运行的另一个终端实例:

xterm -hold -e 'apropos editor' & 

注意-hold。大多数终端在运行完您输入的命令后会退出。关于这个问题,网站上已经有十几个问题了: 另一种选择是使用需要手动退出的应用程序。nano将保持打开状态。如果您只是输出到屏幕,可以将其导入less中:
xterm -e 'apropos editor | less' & 

说到这一点,在你的情况下(就像其他两个人说的那样),似乎更容易的方法是打开另一个终端并运行你的命令。

我可以使用gnome-terminal代替xterm吗?如果可以,应该如何操作? - Pandya
我对如何在xfce4-terminal中实现这个功能很好奇(xfce4-terminal会打开一个新的进程,而xterm则不会——在我的情况下,我实际上并不想要一个新的进程)。 - Brōtsyorfuzthrāx

在Ubuntu 18.04 LTS之后,你可能想要从-e切换到--,即从gnome-terminal -e改为gnome-terminal --,因为-e-x都已经过时了。

  1. 打开两个终端。
  2. 使用 tty 命令识别每个终端。
  3. 假设它们被识别为 /dev/pts/0/dev/pts/1
  4. 在终端 pts/0 上使用 exec 命令将标准输出重定向到 pts/1:exec 1>/dev/pts/1
  5. 现在,从 pts/0 终端的每个命令标准输出都会显示在 pts/1 终端中。
  6. 使用命令将标准输出重定向回去:exec 1>/dev/pts/0
  7. 现在,pts/0 的标准输出恢复正常。

你可以使用以下方式将-e选项应用于gnome-terminal:
gnome-terminal -e 'sh -c propose editor'

这里的sh是gnome-terminal打开的shell。请注意,一旦命令执行完毕,终端将会退出。更多信息,请参考gnome-terminal的手册页面

如何保持新打开的终端在命令执行完毕/终止后不自动退出? - Pandya

这是我对《终结者》的50美分看法:
terminator -x "script.sh; bash"

这也可以,但我无法告诉你其中的区别:

terminator -e "script.sh; bash"

请注意,脚本也可以是一个命令。
在Ubuntu 18.04 LTS上进行了测试。

所以我知道这可能晚了7年,但对于其他人来说,这仍然应该有帮助。我也遇到了这个确切的问题,但找到了一个比其他人提出的解决方案更好的方法。
根据“man gnome-terminal”,你只需执行“gnome-terminal -- sh -c ''”。
“-- sh”允许它运行实际的脚本而不是应用程序,所以它就像在正常窗口中输入一样。不过,需要注意的是,命令执行完毕后终端会退出。
希望这能帮到你!