Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print bash script result behind prompt in the next line

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?

like image 918
NaN Avatar asked Jan 15 '16 12:01

NaN


People also ask

How do you continue to the next line in bash?

From the bash manual: The backslash character '\' may be used to remove any special meaning for the next character read and for line continuation.

How do you continue a command in the next line?

The Windows command prompt (cmd.exe) allows the ^ (Shift + 6) character to be used to indicate line continuation.

What is $@ in bash?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc.


1 Answers

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; } &
like image 110
dan4thewin Avatar answered Oct 13 '22 03:10

dan4thewin