read
命令用于在标准输入中读取数据并赋值给参数
简单的例子:
bash-3.2$ read a
hello world
bash-3.2$ echo $a
hello world
bash-3.2$ read a b # 可以同时为多个变量赋值,输入值用空格分隔
hello world
bash-3.2$ echo $a
hello
bash-3.2$ echo $b
world
-p
选项指定提示语:
bash-3.2$ read -p '输入一个数字:' num
输入一个数字:123
bash-3.2$ echo $num
123
-r
选项转义 \
反斜杠为普通字符:
bash-3.2$ cat a.txt
hello\nworld
bash-3.2$ read a < a.txt
bash-3.2$ echo $a
hellonworld
bash-3.2$ read -r a < a.txt
bash-3.2$ echo $a
hello\nworld
-a
选项创建数组:
bash-3.2$ read -a var
a b c d e
bash-3.2$ echo ${var[*]}
a b c d e
网友评论