Introduction
Input lets a script ask you questions and get your answers. This makes the script interactive, like a conversation.
read variable_name
read name
echo "Hello, $name!"echo "Enter your age:" read age echo "You are $age years old."
#!/bin/bash echo "What is your favorite color?" read color echo "Wow! $color is a nice color."
read in a bash script make it interactive?read doesread command waits for the user to type something and press Enter.read to interactivityread pauses for input = A [OK]read speeds up the scriptread skips errorsname?read usage with prompt-p option with read shows a prompt before waiting for input.read -p "Enter your name: " name is correct.-p = D [OK]read -p to prompt and read input [OK]input instead of readJohn when prompted?
#!/bin/bash read -p "Enter your name: " name echo "Hello, $name!"
read command behaviorname.name and an exclamation mark. If user types "John", it prints "Hello, John!".name prints greeting = B [OK]#!/bin/bash read "Enter a number: " num echo "You entered: $num"What is the main problem?
read syntaxread command requires the prompt string to be passed with the -p option, like read -p "prompt" variable.read "Enter a number: " num which is invalid syntax; it should be read -p "Enter a number: " num.read -p for prompt = C [OK]-p option with read [OK]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
read -p "Enter age: " age to prompt and store input. Other options misuse syntax.read -p before variable for input [OK]-p optionPass the test and this pattern moves unknown