read命令用于从终端或文件中读取用户输入,它读取整行输入,如果没有指定名称,读取的行被赋值给内部变量REPLY。
read命令常用选项:-a,-p,-s,-t,-n
$read
hello
$echo $REPLY
hello
$read answer
hello
$echo $answer
hello
$read first second third
chen xiaopang panda
$echo $first $second $third
chen xiaopang panda
$read -p "Enter your name:" name
Enter your name:chenxiaopang
$echo $name
chenxiaopang
$read -a friends
Tom Mike Jack
$echo ${friends[*]}
Tom Mike Jack
$read -t 5 choice //限定5秒钟内输入变量值,否则,不管用户是否输入,read命令返回非零值
6、-n选项指定读入的字符数目,当达到指定数目时,read命令返回$read -n1 -p 'Enter your Choice (y/n): ' choice
$echo $choice
y
$read -s name
8、从文件读入cat test.txt | while read line
do