read常用几个选项
-n:read -n 2 var,将输入的字符串截取前2个字符放入到变量var中
root@ubuntu:~# read -n 2 var
78root@ubuntu:~# echo ${var}
78
root@ubuntu:~#
-p:read -p “input your name:” name, -p后面是提示字符串,通过输入将值存取到name变量中
注意:“”提示字符串与name变量之间有空格,不然会出现另外情况
root@ubuntu:~# read -p "input your name:" name
input your name:zhangsan
root@ubuntu:~# echo ${name}
zhangsan
root@ubuntu:~#
-s:read -s var,将输入值存储到var中,但不在终端显示,不会显
#!/bin/bash
read -p "input your DBName:" name
echo
read -s -p "input your password:" password
echo
echo ${name}
echo ${password}
root@ubuntu:~/shell# ./no_echo.sh
input your DBName:zhangsan
input your password:
zhangsan
abcd1234
root@ubuntu:~/shell#
-t:read -t 3 var,在3秒内输入数据到var变量中,不然退出
-d:read -d “|” 使用“|”作为输入结束标志
root@ubuntu:~# read -d "|" var
hello|root@ubuntu:~#
root@ubuntu:~# echo ${var}
hello
root@ubuntu:~#
一般-p、-t、-s可以结合使用