0
0
Bash Scriptingscripting~10 mins

Prompting with read -p in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Prompting User Input with read -p in Bash
📖 Scenario: You are creating a simple script that asks a user for their favorite fruit and then shows a message with their answer.
🎯 Goal: Build a Bash script that prompts the user to enter their favorite fruit using read -p and then prints a message including their input.
📋 What You'll Learn
Use read -p to prompt the user with the exact text: "Enter your favorite fruit: "
Store the user input in a variable called fruit
Print the message: "Your favorite fruit is: <fruit>" where <fruit> is the user input
💡 Why This Matters
🌍 Real World
Scripts often need to ask users for information like names, choices, or passwords. Using <code>read -p</code> makes this easy and interactive.
💼 Career
Knowing how to prompt users in shell scripts is useful for system administrators, DevOps engineers, and anyone automating tasks on Linux or Unix systems.
Progress0 / 4 steps
1
Create a variable to store user input
Write a line of code that uses read -p to prompt the user with the text "Enter your favorite fruit: " and stores the input in a variable called fruit.
Bash Scripting
Need a hint?

Use read -p "prompt text" variable to ask the user and save their answer.

2
Add a variable to hold a greeting message
Create a variable called greeting and set it to the exact string "Your favorite fruit is:".
Bash Scripting
Need a hint?

Assign the string to greeting using = without spaces.

3
Combine greeting and user input in a message
Write a line of code that creates a variable called message which combines the greeting variable, a space, and the fruit variable.
Bash Scripting
Need a hint?

Use double quotes and $ to insert variables inside the string.

4
Print the final message
Write a line of code that prints the message variable to the terminal using echo.
Bash Scripting
Need a hint?

Use echo "$message" to show the combined message.

Try entering apple when prompted to test.