Unix Power ToolsUnix Power ToolsSearch this book

36.20. Turn Off echo for "Secret" Answers

When you type your password, Unix turns off echoing so what you type won't show on the screen. You can do the same thing in shell scripts with stty -echo.

stty Section 5.7, read Section 35.18

+#!/bin/sh
   ...
trap 'stty echo; exit' 0 1 2 3 15
# use the right echo for your Unix:
echo "Enter code name: \c"
#echo -n "Enter code name: "
stty -echo
read ans
stty echo
   ...

The response is stored in $ans. The trap (Section 35.17) helps to make sure that, if the user presses CTRL-c to abort the script, characters will be echoed again.

-- JP



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.