[Bash] read command

Zhentiw發表於2024-05-16

The read command is used to take input from the user.

Reading a single input value:

echo "Enter your name:"
read NAME
echo "Hello, $NAME!"

Output after entering "Alice":

Enter your name:
Alice
Hello, Alice!

Reading multiple input values:

echo "Enter your first name and last name:"
read FIRST_NAME LAST_NAME
echo "Hello, $FIRST_NAME $LAST_NAME!"

Output after entering "Alice Smith":

Enter your first name and last name:
Alice Smith
Hello, Alice Smith!

Using a prompt with the -p option:

read -p "Enter your age: " AGE
echo "You are $AGE years old."

Output after entering "30":

Enter your age: 30
You are 30 years old.

相關文章