Alex Cohen Alex Cohen - 4 months ago 9
Linux Question

How to 'read -s' in shell?

I know that user input can be read silently using bash with

read -s someVar
and I was wondering if there is a
/bin/sh
equivalent that allows user input without displaying it on the command line?

Note: I am just curious if
/bin/sh
read
supports this feature somehow.

Answer

Use the stty command to turn off echoing of typed characters.

get_entry () {
  printf "Choose: "
  stty -echo
  IFS= read -r choice
  stty echo
  printf '\n'
}

get_entry

printf "You chose %s\n" "$choice"
Comments