Linux read命令详解

1.最简单的read,从标准输入读取,将结果保存在变量REPLY中

bash 复制代码
# read 
haha
# echo $REPLY
haha
# 

2.read从标准输入读取一行数据,并将其split,再将split后的字段赋值给read命令最后指定的变量,

第一个字段赋值给第一个变量,第二个字段赋值给第二个变量,以此类推。

下面的例子中,从标准输入读取一行,split后将第一个字段的值赋值给yourname变量,即Gilbert,

将第二个字段的值赋值给yourage变量,即30,split默认的分隔符是IFS变量的值,这个值默认是空格

bash 复制代码
# read yourname yourage
Gilbert 30
# echo $yourname
Gilbert
# echo $yourage
30
#

3.如果是交互式程序,让用户输入时最好给出一点提示:

bash 复制代码
# read -p "input your name and age, split with space: " yourname yourage
input your name and age, split with space: Tom 31
# echo $yourname
Tom
# echo $yourage
31
#

4.如果输入的行按照分隔符来分割后字段数大于指定的变量数,则多出来的字段全部赋值给最后一个变量

bash 复制代码
# read -p "input your name and age, split with space: " yourname yourage
input your name and age, split with space: James 39 Lakers
# echo $yourname
James
# echo $yourage
39 Lakers
#

5.如果提示用户输入密码之类的内容,就不宜将其显示在控制台,-s选项可以用在这种场景下

bash 复制代码
# read -p "input your password: " password
input your password: 123456
# echo "your password is: $password"
your password is: 123456
#

# read -s -p "input your password: " password
input your password: # 
# echo "your password is: $password"         
your password is: 951753
#

可以看到,输入的密码没有显示出来

6.默认遇到换行符read命令返回,可以限制读入的总字符数,当输入的总字符数达到后read立即返回,

bash 复制代码
# read -p "input your firstname and lastename, split with space: " firstname lastname 
input your firstname and lastename, split with space: Allen LongestLastNameEver
# echo $firstname
Allen
# echo $lastname
LongestLastNameEver
#

-n 选项可以用于指定读入的字符数

bash 复制代码
# read -p "input your firstname and lastename, split with space: " -n 10 firstname lastname 
input your firstname and lastename, split with space: abcde abcd]#
# echo $firstname
abcde
# echo $lastname
abcd
#
  1. 指定结束符,默认遇到换行符结束读取,read返回,通过-d可以指定新的结束符
bash 复制代码
# read -d "$" -p "input your name and age,split with spaceand stop with $ :" name age  
input your name and age,split with spaceand stop with $ :Smart 25$ # 
# echo $name
Smart
# echo $age
25
# 
  1. 使用其他字符作为分隔符
bash 复制代码
# IFS=";"
# read  -p "input your name and age,split with semicolon(;) :" name age
input your name and age,split with semicolon(;) :Smart;23
# echo $name 
Smart
# echo $age
23
# 

9.禁止转义,-r

bash 复制代码
# read  -p "input your name and age,split with space :" name age
input your name and age,split with space :"Han\"" 30
# echo $name
"Han""
# echo $age
30
#

# read -r -p "input your name and age,split with space :" name age
input your name and age,split with space :"Ham\"" 51
# echo $name
"Ham\""
# echo $age
51
#

10.将数据读入数组

bash 复制代码
# read -p "input 3 numbers, split with space: " -a myarray                                  
input 3 numbers, split with space: 12 13 34
# echo "the first is ${myarray[0]}, the second is ${myarray[1]}, the third is ${myarray[2]}"
the first is 12, the second is 13, the third is 34
# 

11.使用默认值

bash 复制代码
read -e -i "Dav 25" -p "input your name and age,split with space :" name age
input your name and age,split with space :Dav 25
# echo $name
Dav
# echo $age
25

12.从字符串读取

bash 复制代码
#str="Tompthon 67"
#read name age <<< $str
#echo $name
Tompthon
#echo $age
67
#
相关推荐
热爱嵌入式的小许14 分钟前
Linux基础项目开发1:量产工具——显示系统
linux·运维·服务器·韦东山量产工具
韩楚风4 小时前
【linux 多进程并发】linux进程状态与生命周期各阶段转换,进程状态查看分析,助力高性能优化
linux·服务器·性能优化·架构·gnu
陈苏同学4 小时前
4. 将pycharm本地项目同步到(Linux)服务器上——深度学习·科研实践·从0到1
linux·服务器·ide·人工智能·python·深度学习·pycharm
Ambition_LAO4 小时前
解决:进入 WSL(Windows Subsystem for Linux)以及将 PyCharm 2024 连接到 WSL
linux·pycharm
Pythonliu74 小时前
茴香豆 + Qwen-7B-Chat-Int8
linux·运维·服务器
你疯了抱抱我4 小时前
【RockyLinux 9.4】安装 NVIDIA 驱动,改变分辨率,避坑版本。(CentOS 系列也能用)
linux·运维·centos
追风赶月、4 小时前
【Linux】进程地址空间(初步了解)
linux
栎栎学编程4 小时前
Linux中环境变量
linux
我是哈哈hh5 小时前
专题十_穷举vs暴搜vs深搜vs回溯vs剪枝_二叉树的深度优先搜索_算法专题详细总结
服务器·数据结构·c++·算法·机器学习·深度优先·剪枝
郭二哈5 小时前
C++——模板进阶、继承
java·服务器·c++