Is there a POSIX shell alternative to read -e?
readline
is a GNU project (developed alongside bash
). There are other alternatives like BSD libedit, and all POSIX shells have their own line editor, either specific to the shell or based on either of those libraries that implement at least a vi
editing mode (the only one specified by POSIX, though most also support an emacs
mode (not specified by POSIX because RMS objected to it)).
POSIX however only specifies that line editing mode for the shell command line, not for read
.
ksh93
does support it with read
though (provided stdin and stderr are on a terminal device). There, you can do:
set -o emacs
IFS= read -r 'line?prompt: '
The zsh
equivalent is with the vared
(variable editor) builtin:
line=; vared -p 'prompt: ' line
That's the most feature rich with its history handling and the complete customisation of key binding and completion.
read -e
is bash
specific.
IFS= read -rep 'prompt: ' variable
There is no POSIX equivalent. POSIXly, you could start vi
(specified by POSIX) to edit a temporary file and read the content of that file into a variable.
Alternatively, you could look for the availability of one of zsh
/bash
/ksh93
or rlwrap
or other wrappers around libreadline, or socat
(provided it has been built with readline
support) and use any of those if available to read the line or revert to plain read
or vi
if not.
Or use that LE
line-editor function seen in this similar Q&A that implements a limited emacs
-like line editor.
The POSIX read
only supports -r
(to be able to read backslashes). There is no way to ask the user for input and provide command line-like editing, unless you fire up an editor for a file that you later parse.