How do I prompt a user for confirmation in bash script?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
When writing scripts in Bash, you often need situations where user confirmation is needed before proceeding with an operation. This can be crucial for operations that are destructive in nature (like deleting files) or those that require a definite agreement from the user. In this article, we provide different methods to prompt for user confirmation and effectively handle the response in a Bash script.
Using read Command
The simplest approach to get user input in Bash is with the read command. This command pauses script execution and waits for user input.
Basic Confirmation Prompt
Here is a basic example of how to use read to prompt for a yes/no confirmation:
In this script:
read answerstores the user input into the variableanswer.- The
casestatement then checks the first character of the input. It allows any word starting with 'Y' or 'y' to continue, and any starting with 'N' or 'n' to abort. Any other input results in an "Invalid input" message.
Improving the User Experience
To refine the user experience, you can use:
-pto add a prompt message directly in thereadcommand.-n 1to accept a single character input, so that the user doesn’t need to hit ENTER.-rto prevent backslashes from being interpreted as escape characters.
Example:
In this script:
- The use of
[[$REPLY =~ ^[Yy]$]]checks if the input is either 'Y' or 'y'.
Making It a Function
For reusability, encapsulate confirmation logic into a function:
This function repeats the prompt until a valid response is received, which can be particularly useful for critical operations.
Best Practices and Accessibility
Error Handling and Validation
Ensure to validate the input comprehensively to handle unexpected inputs and behavior, which can be enhanced by specific regular expressions or additional case scenarios.
Internationalization
For scripts used in multiple regions, consider internationalizing the "yes" and "no" responses to match local languages and conventions.
Visual Feedback
For crucial operations, provide clear, visual feedback about the action that was confirmed. Optionally, implement a timed out default action using read -t.
Summary Table
| Feature | Option | Description |
| Prompt customization | -p | Prompts user with inline text. |
| Input limit | -n 1 | Accepts a specific character count. |
| Raw input | -r | Disables escape character interpretation. |
| Timed response | -t <sec> | Limits wait time for user input. |
By adopting these strategies, you can enhance both the user experience and the robustness of your bash scripts when confirming critical operations.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.