An interactive script asks the user for information while it runs, instead of running all by itself without stopping.
Click to reveal answer
beginner
How does the read command in bash make a script interactive?
The read command pauses the script and waits for the user to type something. This input can then be used later in the script.
Click to reveal answer
beginner
Why is user input important in scripts?
User input lets scripts do different things depending on what the user wants, making scripts flexible and useful in many situations.
Click to reveal answer
beginner
What happens if a script does not use input commands?
The script runs automatically without stopping, using only the information it already has. It cannot change behavior based on user choices.
Click to reveal answer
beginner
Give an example of a simple interactive bash script.
Example:
#!/bin/bash
echo "What is your name?"
read name
echo "Hello, $name!"
This script asks for your name and then greets you.
Click to reveal answer
What command in bash is commonly used to get user input?
Aecho
Bread
Cls
Dcd
✗ Incorrect
The read command waits for user input and stores it in a variable.
Why would you want a script to be interactive?
ATo make the script shorter
BTo run faster without stopping
CTo ask the user for information during the script run
DTo avoid using variables
✗ Incorrect
Interactivity allows the script to get information from the user and act accordingly.
What happens if a script uses no input commands?
AIt runs without stopping and uses preset data
BIt waits for user input anyway
CIt crashes immediately
DIt asks for input by default
✗ Incorrect
Without input commands, the script runs automatically using only the data it has.
Which of these is NOT a reason to make a script interactive?
ATo customize behavior based on user input
BTo make the script flexible
CTo get information from the user
DTo automate repetitive tasks without user help
✗ Incorrect
Automating tasks without user help means non-interactive scripts.
In the example script: read name, what does name represent?
AA variable to store user input
BA command to print text
CA file name
DA script name
✗ Incorrect
name is a variable that holds what the user types.
Explain in your own words why input makes a script interactive.
Think about how a conversation works between you and the script.
You got /3 concepts.
Describe a simple scenario where an interactive script is better than a non-interactive one.
Imagine ordering coffee with or without telling the barista your preferences.
You got /4 concepts.
Practice
(1/5)
1. Why does using read in a bash script make it interactive?
easy
A. Because it skips errors during execution
B. Because it runs the script faster
C. Because it automatically saves output to a file
D. Because it pauses the script to get input from the user
Solution
Step 1: Understand what read does
The read command waits for the user to type something and press Enter.
Step 2: Connect read to interactivity
Since the script waits for user input, it becomes interactive, asking questions and getting answers.
Final Answer:
Because it pauses the script to get input from the user -> Option D
Quick Check:
read pauses for input = A [OK]
Hint: Input pauses script to ask user questions [OK]
Common Mistakes:
Thinking read speeds up the script
Confusing input with output saving
Believing read skips errors
2. Which of these is the correct syntax to prompt the user and read their input into a variable named name?
easy
A. read -p "Enter your name: " name
B. input "Enter your name: " > name
C. echo "Enter your name: " | read name
D. read name "Enter your name: "
Solution
Step 1: Identify the correct read usage with prompt
The -p option with read shows a prompt before waiting for input.
Step 2: Check variable assignment
The variable name comes after the prompt option, so read -p "Enter your name: " name is correct.
Final Answer:
read -p "Enter your name: " name -> Option A
Quick Check:
Prompt with -p = D [OK]
Hint: Use read -p to prompt and read input [OK]
Common Mistakes:
Using input instead of read
Placing prompt after variable name
Piping echo into read incorrectly
3. What will be the output of this script if the user types John when prompted?
#!/bin/bash
read -p "Enter your name: " name
echo "Hello, $name!"
medium
A. Hello, !
B. Enter your name: John
C. Hello, John!
D. Error: name not defined
Solution
Step 1: Understand the read command behavior
The script prompts the user with "Enter your name: " and waits for input. The input is stored in variable name.
Step 2: Check the echo output
The echo command prints "Hello, " followed by the value of name and an exclamation mark. If user types "John", it prints "Hello, John!".
Final Answer:
Hello, John! -> Option C
Quick Check:
Input stored in name prints greeting = B [OK]
Hint: Input stored in variable prints in echo [OK]
Common Mistakes:
Ignoring variable substitution in echo
Expecting prompt text as output
Assuming variable is empty or undefined
4. This script is supposed to ask for a number and print it, but it doesn't work as expected:
#!/bin/bash
read "Enter a number: " num
echo "You entered: $num"
What is the main problem?
medium
A. Echo command is missing quotes
B. The prompt string should be after -p option in read
C. Variable name cannot be num
D. Script needs sudo to run read
Solution
Step 1: Check the read syntax
The read command requires the prompt string to be passed with the -p option, like read -p "prompt" variable.
Step 2: Identify the error in the script
The script uses read "Enter a number: " num which is invalid syntax; it should be read -p "Enter a number: " num.
Final Answer:
The prompt string should be after -p option in read -> Option B
Quick Check:
Use read -p for prompt = C [OK]
Hint: Prompt must use -p option with read [OK]
Common Mistakes:
Putting prompt string without -p
Thinking variable names are restricted
Believing echo needs sudo
5. You want to write a script that asks the user for their age and then prints if they are an adult or not. Which script correctly uses input to make it interactive and works as expected?
Option A:
read -p "Enter age: " age
if [ $age -ge 18 ]; then
echo "Adult"
else
echo "Not adult"
fi
Option B:
read age "Enter age: "
if [ $age -ge 18 ]; then
echo "Adult"
else
echo "Not adult"
fi
Option C:
read -p age "Enter age: "
if [ $age -ge 18 ]; then
echo "Adult"
else
echo "Not adult"
fi
Option D:
echo "Enter age: " | read age
if [ $age -ge 18 ]; then
echo "Adult"
else
echo "Not adult"
fi
hard
A. Option A
B. Option B
C. Option C
D. Option D
Solution
Step 1: Check prompt and read syntax
The correct usage is read -p "Enter age: " age to prompt and store input. Other options misuse syntax.
Step 2: Verify conditional logic
All options use the same if condition, which is correct. The difference is only in input reading.
Final Answer:
Option A -> Option A
Quick Check:
Correct prompt and read syntax = A [OK]
Hint: Use read -p before variable for input [OK]
Common Mistakes:
Placing prompt after variable name
Using pipe incorrectly with read
Misplacing -p option
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.