Bash – Echo text as suggested prompt in bash

bashline-editor

Is it possible to change text that appears after $PS1. It is what user inputs. I want to suggest some command hereafter my_function is run. Of Course I should be able to modify/delete it using backspace key

root@linux: 
root@linux: ls
aplha beta gamma
root@linux: my_function
root@linux: echo_something_here (It should be deletable by me)

Best Answer

Based on this answer you can use expect (you might have to install it first):

#!/usr/bin/expect -f

# Get a Bash shell
spawn -noecho bash

# Wait for a prompt (in my case was '$', but your example only put ':')
expect ": "

# store the first argument in a variable
set arg1 [lindex $argv 0]

# Type something
send $arg1

# Hand over control to the user
interact

exit

Now you can call it (assuming you saved it as my_function):

root@linux: ./my_function "some text here"
root@linux: some text here

The only undesirable effect might be that it starts a new bash.

Related Question