3

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 Ctrl-C 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]
johnbot
  • 31
  • I think that what you want isn't possible, I understand that you want to "autocomplete" a command so the user only has to press intro (correct me if I am wrong). So you cant autocomplete a code after finishing the script. – Hola Soy Edu Feliz Navidad Oct 09 '12 at 12:23
  • 1
    Why bother if you need to type it for the echo command anyway? There's almost certainly a better solution for your root problem, so tell us what that is. – Kevin Oct 09 '12 at 12:31

3 Answers3

1

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.

jofel
  • 26,758
1

There are a few control sequences that you can send to a terminal, and it will simulate some input. You cannot directly simulate any input, however: this would not be particularly useful, and it would be dangerous to generate arbitrary input when displaying a file that contains control characters.

Most such control sequences (Device Status Report, mouse position reporting, etc.) send back control sequences that are harmless if typed. Some traditional terminals (including older versions of xterm) had a control sequence to send back the window title, which can be set by another control sequence, but modern terminals ignore this command.

To cut a long story short, this is not possible, precisely to avoid what you're trying to do.

0

With zsh.

zle-line-init() {
  ((SECONDS - last < 300)) && return
  LBUFFER="rm -R ./... "
  last=$SECONDS
  zle -R
}
zle -N zle-line-init

(to do it (at most) every 5 minutes).

I don't like the idea of a command appearing suddenly in my prompt though.