4. 输入输出
4. 输入输出
read 命令(输入)
read [-参数]
简单使用
read a
read b
参数
-p:给出提示内容,不支持"\n"换行符
```bash
编写代码
read -p "请输入变量a的值:" a
运行结果
请输入变量a的值:
* -s:隐藏输入的内容
```bash
# 例如密码录入
read -s pwd
echo "你输入的内容是:$pwd"
-t:给出等待时间,超过等待时间会跳过录入
read -t 4 a echo $a echo "hello wordl"
-n:限制读取字符的个数,达到临界值会自动执行
read -n 5 str echo "$str"
-a:按数组格式读入
read -a arrname echo ${arrname[0]}
-i:当做整数Integer
declare -i num = 10+20+30 echo num #60
综合示例
模拟用户登录(结合提示与密码隐藏输入)
read -p "用户名:" username read -p "密码:" -s password echo "你输入的用户名$username" echo "你输入的密码:$password"
echo
echo string
普通输出
echo "hello world"
显示转义字符
echo "\"hello world \""
显示变量
echo "a is $a"
显示换行符
# 使用-e 开启转义
echo -e "a \n d"
显示不换行
# -e 开启转移
# -c 不换行
echo -e "abd\c"
echo "bcd"
原样输出
# 使用单引号''
echo '\n\b\v#a$a$vc'
输出执行结果
echo `date`
Last updated
Was this helpful?