How to echo (a (executable-)string) to the prompt, so that the cursor flashes at the end of the line

command lineterminal

How to echo (a (executable-)string) to the prompt to make the cursor sit at the end of the line?

So that I can hit Enter to execute or CtrlC to throw away the line.

Is this possible at all?

I know an (interactive) bash-script would probably be nicer, but I'd like to keep it simple.

Example:

echo_to_prompt "rm -R ./tmp/logs/delete_me_every_once_in_a_while/"

would result in:

user@machine:~$ rm -R ./tmp/logs/delete_me_every_once_in_a_while/[CURSOR]

Best Answer

If you need the echo_to_prompt() command in a shell script or on the normal command line, you can simply use read:

#!/bin/sh

echo_to_prompt() {
        echo -n "$USER@$HOSTNAME:$PWD $" $@
        read && $@
}

echo_to_prompt rm -R ./tmp/logs/delete_me_every_once_in_a_while/

You can add the echo_to_prompt() function to your .bashrc/.profile/... if you want it on the command line.