10. Reading and Printing Data

In This Chapter

The read Command

The printf Command

Exercises

In this chapter you’ll learn how to read data from the terminal or from a file using the read command and how to print 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 read to the first variable listed in variables, the second word read 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. So for example, the command

read x y

reads a line from standard input, ...

Get Unix Shell Programming, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.