Reading data from user in linux shell script

You can take input from the user using read command. Below program will prompt you to enter your name and once you enter your name, it will be printed back on to the screen.

echo “Please enter your name “
read your_name
echo “Your name is : $your_name”

We can read the data from standard input and initialize multiple variables as well

read name age city
echo “name = $name age = $age city = $city”

When we do not specify the name of variable, read command will assign the value to special variable “REPLY”

read
echo “You entered  $REPLY”

To prevent new lines, you can use “-en” option with echo command.

echo -en “Please enter your name “
read your_name
echo “Your name is : $your_name”

Using different field separator

[email protected]
echo “Enter the 2 values delimited by @ and then hit enter key”
read a b

echo “Value of a is $a”

echo “Value of b is $b”

 

You may also like...