O'Reilly logo

Shell Programming in Unix, Linux and OS X, Fourth Edition by Patrick Wood, Stephen G. Kochan

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

9. Reading and Printing Data

In this chapter you’ll learn how to read data from the terminal or from a file using the read command and how to write formatted data to standard output using the printf command.

The read Command

The general format of the read command is

read variables

When this command is executed, the shell reads a line from standard input and assigns the first word to the first variable listed in variables, the second word to the second variable, and so on. If there are more words on the line than there are variables listed, the excess words get assigned to the last variable. For example, the command

read x y

reads a line from standard input, storing the first word in variable x, and the remainder of the line in variable y. It ...

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