I'm looking for a way to create a script that behaves like this:
$ inject "echo test"
$ echo test
but I want the command "echo test" to appear ready to execute without actually running it right away. Is there a specific command or method to accomplish this?
2 Answers
You can achieve this using a function combined with `bind`. Here’s a quick example:
```bash
write-text() {
local str=$1
[[ -t 0 ]] || return 1
str=${str///\}
str=${str//"/"}
bind '"e[0n":"'$str'"'
printf 'e[5n'
}
```
Just make sure to run this in the same context where you define it, or add it to your .bashrc so it loads automatically.
Another approach is to manipulate the `READLINE_LINE` and `READLINE_POINT` variables. It can be a bit tricky, but check out the repository [bashnippets](https://github.com/schorsch3000/bashnippets) where I've implemented something similar. Just keep in mind OP might be asking for a pre-filled command at the prompt, rather than instruction on changing the prompt itself.

That's exactly right! I want the command to be there when I hit enter, not just an alternate prompt. Thanks for clarifying!