To read user input in Bash, you can use the read
command. The read
command allows you to prompt the user for input and store it in a variable for further processing. Here’s an example:
#!/bin/bash
echo "What is your name?"
read name
echo "How old are you?"
read age
echo "Hello, $name! You are $age years old."
In this script, the read
command is used to prompt the user for their name and age. The user’s input for each question is stored in the name
and age
variables, respectively. The script then uses these variables to display a customized message.
When executing this script, the user will see the prompt and can input their answers interactively. The input will be captured and stored in the respective variables.
Here’s an example of the script’s output:
What is your name?
John
How old are you?
25
Hello, John! You are 25 years old.
The read
command allows you to accept user input in a script, enabling interaction and customization. You can use the input for various purposes, such as performing calculations, making decisions, or processing data.