Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 923
Alex Cohen Avatar asked Jul 24 '16 21:07

Alex Cohen


1 Answers

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"
like image 106
chepner Avatar answered Oct 03 '22 04:10

chepner