0
0
Bash Scriptingscripting~5 mins

Why input makes scripts interactive in Bash Scripting

Choose your learning style9 modes available
Introduction
Input lets a script ask you questions and get your answers. This makes the script interactive, like a conversation.
When you want the script to ask your name and greet you.
When you need to choose options in a menu.
When you want to enter a password or secret safely.
When you want to confirm an action before the script continues.
Syntax
Bash Scripting
read variable_name
The read command waits for you to type something and press Enter.
The typed text is saved in the variable_name for the script to use.
Examples
This script asks for your name and then says hello using what you typed.
Bash Scripting
read name

echo "Hello, $name!"
The script prompts you to enter your age and then repeats it back.
Bash Scripting
echo "Enter your age:"
read age

echo "You are $age years old."
Sample Program
This script asks you for your favorite color and then compliments it.
Bash Scripting
#!/bin/bash

echo "What is your favorite color?"
read color
echo "Wow! $color is a nice color."
OutputSuccess
Important Notes
If you run the script, it will pause and wait for your input at the read command.
You can use multiple read commands to get more than one input.
Always prompt the user before read so they know what to type.
Summary
Input makes scripts interactive by letting them ask questions and get answers.
The read command is used to get input from the user.
Prompting before reading input helps users know what to do.