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: ls
aplha beta gamma
root@linux: my_function
root@linux: echo_something_here (It should be deletable by me)
  • About the only way I can think of to do this would be to have a wrapper program controlling the terminal and "typing" the suggested text at an appropriate time. That seems a bit suboptimal though, so I'll see what others come up with. – Steve Bonds Feb 2 '16 at 17:19
  • 2
    The ZSH solution is print -z, e.g. my_function { print "hello world"; print -z echo_something_here } – thrig Feb 2 '16 at 17:27

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


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.


If I understand the question correctly, then having a support script

 read -ei "$*"
 printf "%s\n" "$REPLY"

called for example ~/bin/iedit, then you can say

eval $( ~/bin/iedit "some text here" )

this will output "some text here", which you can edit. Once you have finished editing it then the resulting command will be executed.

This is not a good example of code. Almost always you want to use "$@" rather than "$*", and one should usually try and avoid using eval. You might want to add a history -s $REPLY as well. Putting these together, the support script could be

 read -ei "$*"
 history -s "$REPLY"
 fc -s

and you would invoke it as 'source ~/bin/iedit "some text here"'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.