Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Why input makes scripts interactive in Bash Scripting - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Interactive Script Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this Bash script?
Consider this Bash script that reads user input and prints a greeting. What will it output if the user types Alex and presses Enter?
Bash Scripting
echo "Enter your name:"; read name; echo "Hello, $name!"
A
Enter your name:
Hello, Alex!
BEnter your name:
CHello, Alex!
DHello, $name!
Attempts:
2 left
💡 Hint
Think about what the script prints before and after reading input.
🧠 Conceptual
intermediate
1:30remaining
Why does using 'read' make a Bash script interactive?
Select the best explanation for why the 'read' command makes a Bash script interactive.
A'read' prints output without waiting for input.
B'read' automatically fills variables without user action.
C'read' pauses the script and waits for user input from the keyboard.
D'read' runs commands in the background without interaction.
Attempts:
2 left
💡 Hint
Think about what happens when the script stops and waits.
🔧 Debug
advanced
2:30remaining
Why does this script not wait for input?
This script is supposed to ask for a name and greet the user, but it immediately prints 'Hello, !' without waiting. What is the problem?
Bash Scripting
echo "Enter your name:"; name=$read; echo "Hello, $name!"
AThe variable assignment uses '$read' instead of the 'read' command.
BThe script needs 'read -p' to prompt and read in one step.
CThe 'echo' command is missing quotes around the prompt.
DThe variable 'name' is not declared before use.
Attempts:
2 left
💡 Hint
Check how the script tries to get input into the variable.
🚀 Application
advanced
3:00remaining
How to make a script ask multiple questions interactively?
You want a Bash script to ask the user for their first name and last name separately, then greet them. Which script correctly does this?
Aecho "First name:"; read first last; echo "Hello, $first $last!"
Bread first last; echo "Hello, $first $last!"
Cecho "Enter full name:"; read name; echo "Hello, $name!"
Decho "First name:"; read first; echo "Last name:"; read last; echo "Hello, $first $last!"
Attempts:
2 left
💡 Hint
Think about how to ask questions one by one and store answers separately.
📝 Syntax
expert
2:00remaining
What error does this script produce?
What error will this Bash script produce when run?
Bash Scripting
echo "Enter your age:"; readage; echo "You are $age years old."
ASyntax error near unexpected token `readage`
Bbash: readage: command not found
CYou are years old.
DNo error, script runs fine
Attempts:
2 left
💡 Hint
Look carefully at the command used to read input.

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

  1. Step 1: Understand what read does

    The read command waits for the user to type something and press Enter.
  2. Step 2: Connect read to interactivity

    Since the script waits for user input, it becomes interactive, asking questions and getting answers.
  3. Final Answer:

    Because it pauses the script to get input from the user -> Option D
  4. 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

  1. Step 1: Identify the correct read usage with prompt

    The -p option with read shows a prompt before waiting for input.
  2. Step 2: Check variable assignment

    The variable name comes after the prompt option, so read -p "Enter your name: " name is correct.
  3. Final Answer:

    read -p "Enter your name: " name -> Option A
  4. 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

  1. 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.
  2. 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!".
  3. Final Answer:

    Hello, John! -> Option C
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    The prompt string should be after -p option in read -> Option B
  4. 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

  1. 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.
  2. Step 2: Verify conditional logic

    All options use the same if condition, which is correct. The difference is only in input reading.
  3. Final Answer:

    Option A -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard