Reputation: 8601
I have a Bash script that returns a command. 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?
Upvotes: 9
Views: 482
Reputation: 1184
If you run bash within tmux (terminal multiplexer), you can use its buffer functions to paste a command at your prompt. 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. 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; } &
Upvotes: 4
Reputation: 80931
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.
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).
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.
Upvotes: 0