简体   繁体   English

在下一行中打印提示后面的bash脚本结果

[英]Print bash script result behind prompt in the next line

I have a Bash script that returns a command. 我有一个返回命令的Bash脚本。 I would like to execute the script and let it automatically print the result behind the prompt in the next line. 我想执行脚本并让它自动在下一行的提示后面打印结果。 Replacing the script call in the current line would be an option too. 替换当前行中的脚本调用也是一种选择。 This way I could edit the command before I execute it. 这样我就可以在执行命令之前编辑命令。 Can this be achieved within a terminal with Bash? 这可以在Bash的终端中实现吗?

If you run bash within tmux (terminal multiplexer), you can use its buffer functions to paste a command at your prompt. 如果在tmux (终端多路复用器)中运行bash,则可以使用其缓冲区函数在提示符下粘贴命令。 You can then edit the command before running it. 然后,您可以在运行之前编辑该命令。 Here's a trivial example: 这是一个简单的例子:

#!/bin/bash
tmux set-buffer 'ls -l'
tmux paste-buffer &

Putting the paste-buffer command in the background let's bash output the prompt before the paste happens. 将paste-buffer命令放在后台让let的bash在粘贴发生之前输出提示。 If the paste happens too quickly, you can add a sub-second sleep like so: 如果粘贴发生得太快,您可以添加亚秒级睡眠,如下所示:

#!/bin/bash
tmux set-buffer 'ls -l'
{ sleep .25; tmux paste-buffer; } &

Other than the "use a temporary file" option given in user3035772's comment one other option would be to use the shell's history for this. 除了user3035772注释中给出的“使用临时文件”选项之外,另一个选项是使用shell的历史记录。

Assuming the command that creates the output is a shell command (or you can be sure its output is only the command you want to run later) you can use history -s to store a command in the history and then recall it on the command line to edit it (or use fc to do so). 假设创建输出的命令是shell命令(或者您可以确定其输出只是您稍后要运行的命令),您可以使用history -shistory -s中存储命令,然后在命令行上调用它编辑它(或使用fc这样做)。

history -s 'echo whatever you "want your" command to be'

Then use fc to edit it in your $EDITOR or hit the up arrow or Ctrl-p to load the history item into the current input line. 然后使用fc$EDITOR编辑它或向上箭头或Ctrl-p将历史记录项加载到当前输入行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM