How to add users to Linux through a shell script
Output from "man 1 passwd":
--stdin
This option is used to indicate that passwd should read the new
password from standard input, which can be a pipe.
So to answer your question, use the following script:
echo -n "Enter the username: "
read username
echo -n "Enter the password: "
read -s password
adduser "$username"
echo "$password" | passwd "$username" --stdin
I used read -s
for the password, so it won't be displayed while typing.
Edit: For Debian/Ubuntu users -stdin
won't work. Instead of passwd
use chpasswd
:
echo $username:$password | chpasswd