0
0
Bash Scriptingscripting~10 mins

Prompting with read -p in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
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.
Execution Sample
Bash Scripting
read -p "Enter your name: " name
 echo "Hello, $name!"
This script asks the user for their name and then greets them using the input.
Execution Table
StepActionPrompt DisplayedUser InputVariable 'name' ValueOutput
1Execute read -pEnter your name:
2User types inputEnter your name: Alice
3Input storedEnter your name: AliceAlice
4Execute echoAliceHello, Alice!
💡 Script ends after greeting the user with their input.
Variable Tracker
VariableStartAfter read -pFinal
name"""Alice""Alice"
Key Moments - 3 Insights
Why does the prompt appear before the user types anything?
Because read -p displays the prompt first (see execution_table step 1), then waits for input.
When is the variable 'name' updated with the user input?
The variable 'name' is updated immediately after the user presses Enter (see execution_table step 3).
What happens if the user just presses Enter without typing anything?
The variable 'name' becomes an empty string, so the output would be 'Hello, !' (not shown in this trace).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'name' after step 2?
A"Alice"
B""
Cnull
D"Enter your name: "
💡 Hint
Check the 'Variable 'name' Value' column at step 2 in the execution_table.
At which step does the script output the greeting?
AStep 4
BStep 2
CStep 3
DStep 1
💡 Hint
Look at the 'Output' column in the execution_table.
If the user input was 'Bob' instead of 'Alice', what would be the output at step 4?
AHello, Alice!
BHello, !
CHello, Bob!
DEnter your name:
💡 Hint
Refer to the 'Output' column and imagine changing the 'User Input' value.
Concept Snapshot
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
Full Transcript
This script uses the read command with the -p option to show a prompt message. The user sees the prompt 'Enter your name: ' and types their name. When they press Enter, the input is saved in the variable 'name'. Then the script uses echo to greet the user by printing 'Hello, ' followed by the name they entered. If the user presses Enter without typing anything, the variable 'name' will be empty and the greeting will show no name. This method is a simple way to ask for input in bash scripts.