使用read直接读取输入
[~]$ echo "input something"; read input; echo "your input: $input"
input something
test
your input: test
多输入
[~]$ echo "input something"; read input1 input2; echo "your input: $input1";
input something
a b
your input: a
读取文件,每次读一行
[~]$ while read line;do echo $line;done < test.sh
#!/bin/bashecho "please input a num to select the command"
read command
if [ "$command" = "0" ];then
echo 0
elif [ "$command" = "1" ];then
echo 1
else
echo "more"
fi
为输入读取设置超时时间 -t
read -t 3 text && echo "$text"
# 超时3秒
设置提示信息-p
[~]$ read -p "input something here:" text; echo "$text";
input something here:aaa
aaa
将输入读到一个数组 -a
[~]$ read -a params;echo "${params[1]}";
a b c
b
读取指定个字符-n
[~]$ read -n 5 letter;echo "$letter";
affffaffff
允许使用tab提示-e
不显示输入(密文)-s
[~]$ read -s letter;echo "$letter";
letter
指定换行符-d
read -d delimiter
不解释转义-r
使用IFS分割文本
[~]$ cat text.txt
zhangsan:18:男
lisi:20:女[~]$ read -p "input name:" name;info="$(grep "^$name" text.txt)";IFS=":";read realName age sex <<< "$info";echo "$realName"; echo "$age";echo "$sex";
input name:zhangsan
zhangsan
18
男