read 命令详解

本文介绍了Shell脚本中read命令的使用方法,包括基本用法、提示信息、限制输入长度及时间、隐藏输入等特性,并提供了多个示例帮助理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

read 命令从标准输入中读取一行,并把输入行的每个字段的值指定给 shell 变量

语法选项
-p  read –p “提示语句”,则屏幕就会输出提示语句,等待输入,并将输入存储在REPLY中
-n  read –n 个数
-t  read –t 时间
-s  read -s 选项能够使read命令中输入的数据不显示在监视器上
例子
  1. 从标准输入读取输入并赋值给变量
[xiess@layzj022301 ~]$ read readfile
hello,world!!welcome to shell
[xiess@layzj022301 ~]$ echo $readfile
hello,world!!welcome to shell
[xiess@layzj022301 ~]$
  1. 等待一组输入,每个单词之间使用空格隔开,直到回车结束,并分别将单词依次赋值给这三个读入变量。
[xiess@layzj022301 ~]$ read frist second third
the_one the_two the_three
[xiess@layzj022301 ~]$ echo "$frist" "$second" "$third"
the_one the_two the_three
[xiess@layzj022301 ~]$
  1. REPLY示例
[xiess@layzj022301 ~]$ read -p "Enter your name: "
Enter your name: admin_xiess
[xiess@layzj022301 ~]$ echo $REPLY
admin_xiess
[xiess@layzj022301 ~]$
  1. 关闭显示
[root@cinder01 ~]# cat readfile.sh 
#!/bin/bash
read -s -p "Enter your password: " password
echo
echo "your password is $password"
[root@cinder01 ~]# chmod a+x readfile.sh 
[root@cinder01 ~]# sh readfile.sh 
Enter your password: 
your password is 123456

转载于:https://www.cnblogs.com/xieshengsen/p/6666972.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值