Shell Script for User Management

Create & Delete Users on Linux using Bash
Managing users manually on a Linux system can be time-consuming. Let’s make it faster and more consistent with two easy shell scripts:
✅ One to create users
✅ One to delete users
We'll add error handling, secure password input, and clean prompts — all in one simple guide!
Script 1: create_user.sh — Create a New User
💡 What This Script Does:
Takes username and password as input
Checks if the user already exists
If not, creates the user securely
Handles errors gracefully

📜 Script:
#!/bin/bash
#########################
# Author: Anju
# Date: 12/4/2025
# version: V1
#Write a shell script to create a user
########################################
read -p "Please enter the username: " username #with this command we can take user input from user
read -s -p "Please enter the password: " password # read -s hides the password input
echo # Just moves to a new line after hidden password input
## with this we can check if user already exit or not.
if id "$username" &>/dev/null; then
echo "user already exist"
exit 1 #stops the script right there and signals that something went wrong
else
#Encrypt the password
encrypted_pass=$(openssl passwd -1 "$password")
#Create the user with home directory and encrypted password
sudo useradd -m "$username" -p "$encrypted_pass"
# Check if the user was created successfully
if [ $? -eq 0 ]; then
echo "$username user created successfully"
else
echo "error: failed to create user, kindly check again"
fi
fi

Key points
Password handling:
useradddoes not accept plain-text passwords safely.You must encrypt the password using
openssl passwdor similar.Password encryption:
encrypted_pass=$(openssl passwd -1 "$password")
💡 What this does:
This line:
Takes the user's plain-text password
Encrypts (hashes) it
Stores the encrypted version in a variable called
encrypted_pass
Breaking down the components:
| Part | Meaning |
openssl | A powerful toolkit used for cryptography — part of most Linux systems. |
passwd | A subcommand that generates hashed (encrypted) passwords. |
-1 | Tells openssl to use the MD5-based hashing algorithm (used for compatibility with /etc/shadow format). |
"$password" | The plain-text password entered by the user. |
Security best practice:
read -shides the password input.Avoid printing or echoing sensitive information like passwords.
Check if the user already exists before creating
if id "$username" &>/dev/null; then echo "user already exist" exit 1 fiPurpose: This command checks whether a user with the username stored in the variable
$usernamealready exists on the system.Component Breakdown:
| Part | Meaning | | --- | --- |
idThis is a command that displays the user ID (
uid) and group ID (gid) of a specified username."$username"This passes the value of the
usernamevariable to theidcommand. Using double quotes is good practice in case the variable contains spaces or special characters.&>/dev/nullThis redirects both standard output (
stdout) and standard error (stderr) to/dev/null, which is like a "black hole" — anything sent there is discarded. So this makes the command silent, meaning it won’t print anything on the screen whether the user exists or not.Why use
exit 1here?You don’t want the script to continue if the user already exists.
So
exit 1stops the script right there and signals that something went wrong (in this case, the user already exists).It's a good way to fail gracefully instead of trying to re-create an existing user, which would cause errors.
If you don't use exit 1, then the script would continue running — and possibly try to run useradd, which will fail since the user already exists. That could cause confusing or messy errors.
Script 2: delete_user.sh — Delete an Existing User
💡 What This Script Does:
Takes a username to delete
Checks if the user exists
Deletes the user and their home directory
Handles errors if deletion fails
📜 Script:
vim delete_user.sh
#!/bin/bash
#########################
# Author: Anju
# Date: 12/4/2025
# version: V1
#Write a shell script to create a user
########################################
read -p "Please enter the username you want to delete: " username #with this command we can take username as input from user
echo "" # for space
if id "$username" &>/dev/null; then
sudo userdel -r "$username"
if [ $? -eq 0 ]; then
echo "user deleted successfully"
else
echo "error: try again to delete this user"
fi
else
echo "this user does not exist to delete"
fi
all check points
✅ Taken user input
✅ Checked if the user exists
✅ Deleted the user (with their home directory)
✅ Handled both success and error cases
see the output

output:
userdel: Shree mail spool (/var/mail/Shree) not found
user deleted successfully
🧠 What it means: userdel tries to delete everything related to the user, including:
Home directory (/home/Shree)
Mail spool file (/var/mail/Shree)
In your case, the mail spool file didn't exist, so it showed a non-fatal warning:
userdel: Shree mail spool (/var/mail/Shree) not found
✅ This is not an error — just a message saying that it didn’t find any mail to delete.
🚀 How to Run These Scripts:
Make them executable:
bashCopyEditchmod +x create_user.sh delete_user.shRun the scripts:
bashCopyEdit./create_user.sh ./delete_user.sh
💡 You need
sudoprivileges to run these commands.
done :)



