read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]
If no names are supplied, the line read is assigned to the variable REPLY.
-r Backslash does not act as an escape character. The backslash is considered to be part of the line. In particular, a backslash-newline pair may not be used as a line continuation.
反斜线不作为转义字符起作用。反斜线被当做行数据的一部分。
特别值得注意的是,反斜线-换行 组合将不能作为行接续来使用。
每次调用read命令都会读取文件中的"一行"文本。当文件没有可读的行时,read命令将以非零状态退出。读取文件的关键是如何将文本中的数据传送给read命令。最常用的方法是对文件使用cat命令并通过管道将结果直接传送给包含read命令的while命令 。
#!/bin/bash count=1 cat dat| while read line #cat 命令的输出作为read命令的输入,read读到的值放在line中 do echo "$count:$line" count=$(($count + 1)) done exit 0