O'Reilly logo

Linux and Unix Shell Programming by David Tansley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

5.2. read

Use read to take information from the keyboard or from a line of text from a file and assign it to a variable. If you specify only one variable then the read will assign all input to that variable until it sees an end-of-file marker or the shell sees a carriage return.

The general format is:

read variable1 variable2... 

One variable has been specified, which holds all text until the return key has been hit:

						
$ read name 
Hello I am superman 
$ echo $name 
Hello I am superman
					

Here, we assign two variables to hold a first and a second name. The shell will use the space bar as the separator for each variable.

						
$ read name surname 
John Doe 
$ echo $name $surname 
John Doe
					

If you type in too many fields of text, the shell will assign all ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required