Bash Script to Create User Account with Output
Use the Bash command
sudo useradd username inside a script to create a user account, for example: sudo useradd newuser creates a user named 'newuser'.Examples
Inputnewuser
OutputUser 'newuser' created successfully.
Inputtest123
OutputUser 'test123' created successfully.
Inputadmin
OutputUser 'admin' created successfully.
How to Think About It
To create a user account with a Bash script, first get the username from the user or as an argument. Then run the system command
useradd with that username. Finally, check if the command succeeded and print a message.Algorithm
1
Get the username input from the user or script argument.2
Run the command <code>sudo useradd username</code> to create the user.3
Check if the command was successful.4
Print a success or error message.Code
bash
#!/bin/bash read -p "Enter username to create: " username if sudo useradd "$username"; then echo "User '$username' created successfully." else echo "Failed to create user '$username'." fi
Output
Enter username to create: newuser
User 'newuser' created successfully.
Dry Run
Let's trace creating user 'newuser' through the script
1
Input username
User types 'newuser' when prompted.
2
Run useradd command
Script runs: sudo useradd newuser
3
Check command result
Command succeeds, script prints success message.
| Step | Action | Value |
|---|---|---|
| 1 | Input username | newuser |
| 2 | Run command | sudo useradd newuser |
| 3 | Output message | User 'newuser' created successfully. |
Why This Works
Step 1: Get username input
The script uses read to get the username from the user.
Step 2: Create user with useradd
The useradd command creates the new user account on the system.
Step 3: Check success and print message
The script checks if useradd ran without error and prints a message accordingly.
Alternative Approaches
Using adduser command
bash
#!/bin/bash read -p "Enter username to create: " username if sudo adduser --disabled-password --gecos "" "$username"; then echo "User '$username' created successfully with adduser." else echo "Failed to create user '$username'." fi
adduser is more interactive and user-friendly but may not be available on all systems.
Passing username as script argument
bash
#!/bin/bash username="$1" if [ -z "$username" ]; then echo "Usage: $0 username" exit 1 fi if sudo useradd "$username"; then echo "User '$username' created successfully." else echo "Failed to create user '$username'." fi
This method allows automation by passing the username directly when running the script.
Complexity: O(1) time, O(1) space
Time Complexity
Creating a user is a single system command, so it runs in constant time regardless of input size.
Space Complexity
The script uses minimal memory, only storing the username string and command status.
Which Approach is Fastest?
Using useradd directly is fastest and simplest; adduser adds convenience but may be slower.
| Approach | Time | Space | Best For |
|---|---|---|---|
| useradd command | O(1) | O(1) | Simple, fast user creation |
| adduser command | O(1) | O(1) | User-friendly, interactive creation |
| Script argument input | O(1) | O(1) | Automation and scripting |
Always run user creation scripts with sudo to have the necessary permissions.
Forgetting to run the script with sudo causes permission errors when creating users.