0

I am creating a script that is used for creating an user account. I want to hide the characters of the password while the user is prompted to enter the password

I tried to use read -ps but I get

./account.sh: line 26: read: `Password: ': not a valid identifier

Here is my code

#!/bin/bash

# Check if user is Root
if [[ ${UID} -ne '0' ]]
then
    echo 'You are not autharized for this process'
    exit
fi

# Ask for full name
read -p 'Full Name: ' full_name # Set the input data to "full_name" variable

# Ask for user name
read -p 'User Name: ' user_name # Set the input data to "user_name" variable

# Check for user name length
username_length=${#user_name}

if [[ ${username_length} -lt 3 ]]
then
    echo 'User name should be at least 3 characters'
    exit
fi

# Ask for password
read -ps 'Password: ' password # Set the input data to "password" variable

# Check for password length
password_length=${#password}

if [[ ${password_length} -lt 8 ]]
then
    echo 'Password should be at least 8 characters'
    exit
fi

# Create the user
useradd -c "${comment}" -m ${user_name}

# Set the password for the user
echo -e "$password\n$password" | passwd "$user_name"

# Change password on first login
passwd -e ${user_name}
hosam.shafik
  • 119
  • 2
  • 6
  • `-p` takes an argument. `-s` doesn't. Try `read -sp 'Password: ' password` (or separate it to `read -s -p 'Password: ' password`). – Joachim Sauer Sep 17 '19 at 11:24

1 Answers1

1

for your example you could use

read -p -s 'Password: ' password

ps friendly reminder to never store passwords in plain text

lucas
  • 21
  • 3