read -r -p "put an option: " option
echo $option
this works but shellcheck gives me:
In POSIX sh, read -p is undefined.
How to get user input with a prompt into a variable in a posix compliant way?
read -r -p "put an option: " option
echo $option
this works but shellcheck gives me:
In POSIX sh, read -p is undefined.
How to get user input with a prompt into a variable in a posix compliant way?
You could use read without -p
:
printf "put an option: " >&2
read -r option
printf '%s\n' "$option"
printf
orecho
? I mean, can't this be handled byread
only? thanks – testoflow May 20 '21 at 20:55shellcheck
gives you that warning when you targetsh
, but common implementations (e.g.dash
,busybox ash
) actually support prompting withread -p
. It won't work, however, inzsh
and, I think, in ksh88 and derivatives (and in the original Bourne shell). – fra-san May 20 '21 at 21:10-p
but if you want to be pure POSIX sh you can't use it. If you know it wont be an issue you can ignore certain shellcheck rules. – jesse_b May 20 '21 at 23:52