Read Input From User In Shell Script


We can simply get user input from the read command in BASH. It provides a lot of options and arguments along with it more flexible usage


Read Basic Value from User

#!/bin/bash
read name
echo "Hello ${name}"


#!/bin/bash
read name
read age
echo "Hello ${name}, and your age is ${age}"



Read With Prompt Message

#!/bin/bash
read -p "please enter your name " name
read -p "please enter your age " age
echo "Hello ${name}, and your age is ${age}"



Read Secret Text With Prompt Message

#!/bin/bash
read -p "please enter your password " -s password
echo "your password is ${password}"



let's create a file with below content and execute it.

#!/bin/bash
read -p "please enter your name " name
read -p "please enter your age " age
read -p "please enter your password " -s password
echo "Hello ${name}, and your age is ${age} and your password is ${password}"


output

┌──(gaurav㉿learning-ocean)-[~/shellscript-youtube]
└─$ ./readvariable.sh
please enter your name Gaurav
please enter your age 30
please enter your password
hello Gaurav, and your age is 30,  your password is [email protected]123