Concept Flow - Prompting with read -p
Start Script
Display prompt with read -p
User types input
Input stored in variable
Use input in script
End Script
The script shows a prompt, waits for user input, stores it in a variable, then uses that input.
read -p "Enter your name: " name echo "Hello, $name!"
| Step | Action | Prompt Displayed | User Input | Variable 'name' Value | Output |
|---|---|---|---|---|---|
| 1 | Execute read -p | Enter your name: | |||
| 2 | User types input | Enter your name: | Alice | ||
| 3 | Input stored | Enter your name: | Alice | Alice | |
| 4 | Execute echo | Alice | Hello, Alice! |
| Variable | Start | After read -p | Final |
|---|---|---|---|
| name | "" | "Alice" | "Alice" |
read -p "Prompt" variable - Displays prompt and waits for user input - Stores input in variable - Use variable later with $variable - If no input, variable is empty - Simple way to get user input in bash
read -p command do in a bash script?read -pread -p command shows a prompt message and waits for user input.read -p = prompt and store input [OK]read -p asks and saves input in one step [OK]read -p only prints text without inputread -p with commands that delete filesname?read -pread -p "prompt message" variable.read -p "Enter your name:" name.read -p "prompt" variable [OK]-p comes before the prompt string [OK]-p-name or -promptread -p "Enter your name: " user echo "Hello, $user!"
read -p commanduser.$user becomes "Alice", so the echo prints "Hello, Alice!".read -p Enter your age: age echo "You are $age years old."
read -p syntax-p must be enclosed in quotes to be treated as a single string.read -p Enter your age: age without quotes around "Enter your age:", causing a syntax error.-p [OK]read -p?read -p syntax-p must come before the prompt string and variable.read -p "Favorite color?" color which is correct. Others have wrong order, missing quotes, or invalid flags.read -p "prompt" variable with quotes [OK]-p-promptPass the test and this pattern moves unknown