4

Is it possible to output text to a shell window, via bash script, that is user-editable? I essentially want to pre-fill certain information and give the user the ability to edit it if it's wrong.

For instance, if I were to write in a script:

echo -n "Enter your name: Anthony"

while read user_input
do
    # do stuff with $user_input
done

How can I allow the user to inline edit the word Anthony only (aka, don't allow backspacing past the A in Anthony), and how can I store the value into a variable once the RETURN key is pressed?

EDIT

I'm looking for something similar to the -i option of read (see answer posted here), but this is only available on bash 4+. Is there an alternative for bash 3?

Community
  • 1
  • 1
lobianco
  • 6,226
  • 2
  • 33
  • 48
  • Duplicate of http://stackoverflow.com/questions/226703/how-do-i-prompt-for-input-in-a-linux-shell-script? – emallove Feb 18 '13 at 18:48
  • @emallove Not quite - I know how to read user input. The essence of what I'm trying to uncover is how to echo text that can be edited inline by the user. – lobianco Feb 18 '13 at 18:53
  • Actually, I think I see what you're referring to. The answer by xaccrocheur seems to be what I'm looking for. Can something similar to this be done on bash 3.2.x? – lobianco Feb 18 '13 at 19:01
  • If you want a feature that was added to Bash 4.x, then you have to upgrade to get it — or retrofit it into your own variant of Bash 3.x. No; it isn't going to be available in Bash 3.x. – Jonathan Leffler Feb 18 '13 at 19:22
  • @JonathanLeffler I know _it_ won't be available in 3.x - I was inquiring about an alternative. But thank you for your input. – lobianco Feb 19 '13 at 00:04
  • In theory you could provide an alternative to `read` that could be used like: `user_input=$(alt_read -i "$initial" -p "$prompt")` or thereabouts, and it would use the GNU `readline` library to display the prompt and initial data and echo the final information (as edited) to its standard output so that the shell captures it. It wouldn't be all that dreadfully hard to write, but I don't know of anyone who's written it. If you're sensible, you'll model the alternative on the `bash` 4.x `read` command notation (or a subset thereof), so it is as easy as possible to migrate. – Jonathan Leffler Feb 19 '13 at 00:15
  • Could [NCurses](http://en.wikipedia.org/wiki/Ncurses) help? – emallove Feb 22 '13 at 14:38
  • 2
    What about something like `dialog --no-lines --inputbox 'Enter your name:' 8 50 'Anthony'`? – Cristian Ciupitu May 10 '13 at 21:08
  • on bash 4, `read` with `-i` only works with `-e`. My guess is that u have upgraded it already :>. https://unix.stackexchange.com/a/115308/235896 – VeganEye Jul 14 '20 at 21:55

1 Answers1

1

I needed similar setup recently so what I did was

$ cat a.sh
function input {
  python -c '
import sys,readline
readline.set_startup_hook(lambda: readline.insert_text(sys.argv[2]))
sys.stderr.write(raw_input(sys.argv[1]))
' "$@" 3>&1 1>&2 2>&3
}

A=$( input 'question: ' default )
echo "A='$A'"

$ ./a.sh
question: default
A='default'

Well, it's not actually bash, but it made the job done.

Neuron
  • 363
  • 2
  • 6