Print the bash script result for the prompt in the next line

I have a Bash script that returns a command. I would like to execute a script and automatically print the result for the prompt in the next line. Replacing the script call on the current line will also be an option. That way, I could edit the command before executing it. Can this be achieved in the terminal using Bash?

+6
source share
2 answers

If you run bash in tmux (terminal multiplexer), you can use its buffering functions to insert the command in your prompt. Then you can edit the command before running it. Here's a trivial example:

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

Typing the paste-buffer command in the background allows bash to display a prompt before the paste occurs. If the insert is too fast, you can add extra sleep as follows:

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

In addition to the β€œUse temporary file” option provided in user3035772 comment , another option would be to use the shell history for this.

Assuming the command that creates the output is a shell command (or you can be sure that its output is just the command you want to run later), you can use history -s to store the command in history, and then call it on on the command line to edit it (or use fc ).

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

Then use fc to edit it in $EDITOR or press the up arrow or Ctrl-p to load the history item into the current input line.

0
source

All Articles