Bird
Raised Fist0
Bash Scriptingscripting~10 mins

read command in Bash Scripting - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - read command
Start Script
↓
Prompt User
↓
Wait for Input
↓
Store Input in Variable
↓
Use Variable
↓
End Script
The read command waits for user input, stores it in a variable, then the script uses that input.
Execution Sample
Bash Scripting
echo "Enter your name:"
read name
echo "Hello, $name!"
This script asks for your name, reads it, then greets you using the input.
Execution Table
StepActionInput/ConditionVariable StateOutput
1Print promptN/Aname = ''Enter your name:
2Wait for user inputUser types 'Alice'name = ''
3Store input in variableInput 'Alice'name = 'Alice'
4Print greetingname = 'Alice'name = 'Alice'Hello, Alice!
5End scriptN/Aname = 'Alice'Script ends
💡 Script ends after printing greeting using the stored input.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
name'' (empty)'' (waiting input)'Alice''Alice'
Key Moments - 3 Insights
Why does the script wait after the prompt?
Because the read command pauses execution to wait for user input, as shown in step 2 of the execution_table.
How does the input get stored in the variable?
When the user presses Enter, read assigns the typed text to the variable, as seen in step 3 where name changes from empty to 'Alice'.
What happens if the user presses Enter without typing anything?
The variable will be set to an empty string, so the greeting would say 'Hello, !' because no input was stored.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of the variable 'name'?
A'Alice'
B'' (empty)
C'Enter your name:'
DUndefined
💡 Hint
Check the 'Variable State' column at step 3 in the execution_table.
At which step does the script print the greeting message?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Output' column to find when 'Hello, Alice!' is printed.
If the user inputs 'Bob' instead of 'Alice', what changes in the variable_tracker?
AThe variable 'name' will remain empty
BThe variable 'name' will be 'Alice'
CThe variable 'name' will be 'Bob' after step 3
DThe script will crash
💡 Hint
The variable_tracker shows the variable value after input is stored; it changes to the user input.
Concept Snapshot
read command syntax:
read variable_name

Behavior:
- Pauses script to get user input
- Stores input in the variable
- Continues script using that input

Key rule: User must press Enter to finish input.
Full Transcript
The read command in bash scripting pauses the script to ask the user for input. When the user types something and presses Enter, the input is stored in a variable. The script can then use this variable to perform actions, like printing a greeting. For example, the script prints a prompt, waits for input, stores it, then prints 'Hello' with the input name. If the user presses Enter without typing, the variable is empty. This command is useful for interactive scripts.

Practice

(1/5)
1. What does the read command do in a bash script?
easy
A. It runs another script.
B. It prints text to the screen.
C. It waits for user input and stores it in a variable.
D. It deletes files.

Solution

  1. Step 1: Understand the purpose of read

    The read command pauses the script and waits for the user to type something.
  2. Step 2: Identify what happens with the input

    The input typed by the user is saved into a variable for later use in the script.
  3. Final Answer:

    It waits for user input and stores it in a variable. -> Option C
  4. Quick Check:

    read = wait for input and save [OK]
Hint: Remember: read = get input from user [OK]
Common Mistakes:
  • Thinking read prints output
  • Confusing read with echo
  • Assuming read runs commands
2. Which of the following is the correct syntax to read a user's name into a variable called username?
easy
A. read > username
B. read -p username
C. read $username
D. read username

Solution

  1. Step 1: Recall basic read syntax

    The correct syntax to store input is read variable_name without any special characters.
  2. Step 2: Check each option

    read username uses read username, which correctly stores input in username. Others misuse flags or symbols.
  3. Final Answer:

    read username -> Option D
  4. Quick Check:

    read variable_name = correct syntax [OK]
Hint: Use read followed by variable name only [OK]
Common Mistakes:
  • Adding $ before variable in read
  • Using > instead of variable name
  • Misusing -p without prompt text
3. What will be the output of this script?
echo "Enter your age:"
read age
echo "You are $age years old."
medium
A. You are [user input] years old.
B. You are years old.
C. You are $age years old.
D. Syntax error

Solution

  1. Step 1: Understand script flow

    The script prints a prompt, waits for user input saved in age, then prints a message using that input.
  2. Step 2: Predict output after input

    If user types 25, output will be "You are 25 years old." showing the input value.
  3. Final Answer:

    You are [user input] years old. -> Option A
  4. Quick Check:

    read stores input, echo prints it [OK]
Hint: Input replaces variable in echo output [OK]
Common Mistakes:
  • Expecting literal $age printed
  • Thinking read does not save input
  • Assuming syntax error without reason
4. Identify the error in this script snippet:
read -p "Enter your city: " city
echo "City is $city"
medium
A. The script is correct and will work as expected.
B. The variable name should not be after the prompt.
C. The -p option is not valid with read.
D. Quotes around prompt cause syntax error.

Solution

  1. Step 1: Check usage of read with -p

    The -p option allows showing a prompt before reading input, which is valid in bash.
  2. Step 2: Verify variable placement and quotes

    The variable city is correctly placed after the prompt, and quotes around prompt text are correct.
  3. Final Answer:

    The script is correct and will work as expected. -> Option A
  4. Quick Check:

    read -p "prompt" variable = valid [OK]
Hint: read -p "prompt" variable is correct syntax [OK]
Common Mistakes:
  • Thinking -p is invalid
  • Misplacing variable before prompt
  • Removing quotes around prompt
5. You want to read two values, first and last, from one line of user input separated by space. Which command correctly does this?
hard
A. read -p first last
B. read first last
C. read first; read last
D. read -a first last

Solution

  1. Step 1: Understand reading multiple variables

    The read command can take multiple variable names to split input by spaces.
  2. Step 2: Analyze each option

    read first last correctly uses read first last to read two words into two variables. read -p first last misuses -p. read first; read last reads twice separately. read -a first last uses -a incorrectly here.
  3. Final Answer:

    read first last -> Option B
  4. Quick Check:

    read var1 var2 = split input by space [OK]
Hint: Use read with multiple variables to split input [OK]
Common Mistakes:
  • Using multiple read commands instead of one
  • Misusing -p option for multiple variables
  • Using -a without array handling
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard