Shell scripting (BASH) : How to accept password in script
You can use read command with -s option, which causes input coming from a terminal to not be echoed. This is useful to verify password or something else.
One line is read from the keyboard or from file descriptor FD if the -u option is supplied, and the first word is assigned to the first NAME, the second word to the second NAME, and so on, with leftover words assigned to the last NAME. Only the characters found in $IFS are recognized as word delimiters. If no NAMEs are supplied, the line read is stored in the REPLY variable. If the -r option is given, this signifies `raw' input, and backslash escaping is disabled. The -d option causes read to continue until the first character of DELIM is read, rather than newline. If the -p
option is supplied, the string PROMPT is output without a trailing newline before attempting to read. If -a is supplied, the words read are assigned to sequential indices of ARRAY, starting at zero. If -e is supplied and the shell is interactive, readline is used to obtain the line. If -n is
supplied with a non-zero NCHARS argument, read returns after NCHARS characters have been read. The -s option causes input coming from a terminal to not be echoed.
Following script, demonstrate this concept:
1) Download/view the passwordscript.bash script
2) Save the changes and execute script:
$ chmod +x passwordscript.bash
$ ./passwordscript.bash
Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or RSS feed to get all updates.
You can Email this page to a friend.
You may also be interested in other helpful articles:
- Bash shell script tip: Run commands from a variable
- How to: Check the bash shell script is being run by root or not
- Shell scripting (BASH) : How to create temporary random file name
- Shell scripting (BASH) : Notify admin user if script load crossed certain limit
- BASH Shell Frequently Asked Questions
Discussion on This Article:
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!


Is it possible to put the passwordscript.bash online again?
Hello,
Link is fixed. Sorry for any inconvenience!
http://bash.cyberciti.biz/script/passwordscript.bash.php
Regards,
Vivek