1

I cannot figure out how to insert a string into a bash terminal.

Here is what I would like to achieve:

$./insert.sh
$this was inserted by script[Cursor is here]

I would still like to be able to edit "this was inserted by script". It should not be executed without pressing enter.Like pasting into a terminal will leave the text without executing it (Assuming no newlines).

Gruber
  • 94
  • 4
  • What are you trying to accomplish? – Ulises André Fierro Sep 06 '17 at 14:55
  • @Gruber, ...without getting into nonportable hackery, using something other than an external script (ie. readline configuration or extensions), or running your shell under `expect`, the exact thing you're asking for isn't possible. You can do things that are similar -- creating an editable command that will be run *by your script*, not by the parent shell, for instance. Knowing more about your use case would let us suggest something that suffices for your actual purpose and is feasible to implement in a robust and portable manner. – Charles Duffy Sep 06 '17 at 15:01
  • Also, on SuperUser, see [populate command line from script](http://superuser.com/questions/1023185/populate-command-line-from-script) – Charles Duffy Sep 06 '17 at 15:05

1 Answers1

0

You can use the read builtin:

read -e -p'$ ' -i 'this was inserted by script' var_name
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • 1
    As I understand it, the OP wants that text to be queued up *as the next command to run* (by the parent shell invoking the script, as opposed to by the script itself). I don't see how this accomplishes that. – Charles Duffy Sep 06 '17 at 14:58
  • Yeah, it seems pretty clear to me that he's trying to emulate user input for the shell. – Ulises André Fierro Sep 06 '17 at 14:59
  • @CharlesDuffy Probably I was misreading the question. *this was inserted by the script* didn't sound like a command to me. – hek2mgl Sep 06 '17 at 16:53