Search code examples
linuxbashposixshuser-input

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.


Solution

  • 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"