Introduction
You use read -p to ask the user a question and get their answer right away in a script.
read -p "Your question here: " variable_nameread -p "Enter your name: " nameread -p "Are you sure? (y/n): " answerread -p "Press Enter to continue..."#!/bin/bash read -p "What is your favorite color? " color echo "You chose: $color"
read -p command do in a bash script?read -pread -p command shows a prompt message and waits for user input.read -p = prompt and store input [OK]read -p asks and saves input in one step [OK]read -p only prints text without inputread -p with commands that delete filesname?read -pread -p "prompt message" variable.read -p "Enter your name:" name.read -p "prompt" variable [OK]-p comes before the prompt string [OK]-p-name or -promptread -p "Enter your name: " user echo "Hello, $user!"
read -p commanduser.$user becomes "Alice", so the echo prints "Hello, Alice!".read -p Enter your age: age echo "You are $age years old."
read -p syntax-p must be enclosed in quotes to be treated as a single string.read -p Enter your age: age without quotes around "Enter your age:", causing a syntax error.-p [OK]read -p?read -p syntax-p must come before the prompt string and variable.read -p "Favorite color?" color which is correct. Others have wrong order, missing quotes, or invalid flags.read -p "prompt" variable with quotes [OK]-p-promptPass the test and this pattern moves unknown