How can I set up a script to simulate command execution without running it?

0
24
Asked By CuriousCat87 On

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

Answered By ScriptSavvy23 On

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.

Answered By TechyNerd99 On

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.

CuriousCat87 -

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

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.