Reputation: 109
I'm writing a bash script that will use readline command to catch user's multiline input (list of packages), but I can't figure out how to delete (backslash) characters while inputing text (let's suppose I made a mistake). Below is the part of the code I'm implying.
read -d `echo -e "\e"` -p $'Give me a list:\n' PACKAGES
\e is the escape character to exit multiline input. Here what I got when I try to delete a character (I did tried all the methods I know: alt+backslash,ctrl+w;ctrl+u):
# read -d `echo -e "\e"` -p $'Give me a list:\n' vPACKAGES
Give me a list:
line one
line two
line 3^H^W^U^H^
As you can see ^H is how backslash was intrpreted, ^W =ctrl+w, so I'm unable to delete the any character only escape with escape. I did tried to play with read flags, -e letting me delete characters but enter becomes no longer a line breaker.
Thank you in advance for your advices and help.
Upvotes: 1
Views: 1350
Reputation: 80941
I wouldn't do this this way at all. I would just prompt for values in a loop until you get an empty entry or some other delimiter.
Something like this (untested):
pkgs=()
printf 'Give me a list:\n'
while read -e pkg; [ -n "$pkg" ]; do
# Validate package, etc., etc.
pkgs+=("$pkg")
done
# Use `${pkgs[@]}` ...
You can use any other test instead of [ -n "$pkg" ]
if you have some other terminating entry. (e.g. [ "$pkg" = "." ]
to test for a .
, etc.)
Or, as glenn jackman correctly adds, with newer (4.0+) versions of bash the new readarray
builtin can be used here something like this:
printf 'Enter package names, one per line: hit Ctrl-D on a blank line to stop\n'
readarray -t pkgs
Upvotes: 2