read命令用于接收键盘的标准输入以及其它文件的输入,得到输入后,read命令会将数据放到一个标准变量中。

示例 1:

#!/bin/bash

read -s -p  "Enter you password: " password            #-s:不显示输入,-p:在read命令行指

echo "Hello,your password is $password"                定一个提示


示例 2:

#!/bin/bash

echo -n "Enter your name: "

read name                                              #从键盘输入

echo "Hello! $name"


#!/bin/bash

read -t 5 -p "Enter your name: " name                  #-t:设定时间限制

echo "This is $name"


示例 3:读行

#!/bin/bash

file=/etc/fstab

i=1

cat $file | while read line

do

  echo $i###$line

  ((i++))

done