> For the complete documentation index, see [llms.txt](https://sugar-at.gitbook.io/blog-article/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sugar-at.gitbook.io/blog-article/shell/4.md).

# 4. 输入输出

## 4. 输入输出

### read 命令(输入)

read \[-参数]

简单使用

```bash
read a
read b
```

#### 参数

* -p:给出提示内容,不支持"\n"换行符

  \`\`\`bash

  **编写代码**

  read -p "请输入变量a的值:"  a

## 运行结果

请输入变量a的值:

````
* -s:隐藏输入的内容
```bash
# 例如密码录入
read -s pwd
echo "你输入的内容是:$pwd"
````

* -t:给出等待时间,超过等待时间会跳过录入

  ```bash
  read -t 4 a
  echo $a
  echo "hello wordl"
  ```
* -n:限制读取字符的个数,达到临界值会自动执行

  ```bash
  read -n 5 str
  echo "$str"
  ```
* -a:按数组格式读入

  ```bash
  read -a arrname
  echo ${arrname[0]}
  ```
* -i:当做整数Integer

  ```bash
  declare -i num = 10+20+30
  echo num #60
  ```

  **综合示例**

  模拟用户登录(结合提示与密码隐藏输入)

  ```bash
  read -p "用户名:" username
  read -p "密码:" -s password
  echo "你输入的用户名$username"
  echo "你输入的密码:$password"
  ```

### echo

> echo string

#### 普通输出

```bash
echo "hello world"
```

#### 显示转义字符

```bash
echo "\"hello world \""
```

#### 显示变量

```bash
echo "a is $a"
```

#### 显示换行符

```bash
# 使用-e 开启转义
echo  -e "a \n d"
```

#### 显示不换行

```bash
# -e 开启转移
# -c 不换行
echo -e "abd\c"
echo "bcd"
```

#### 原样输出

```bash
# 使用单引号''
echo '\n\b\v#a$a$vc'
```

#### 输出执行结果

```bash
echo `date`
```

### [printf](https://www.runoob.com/linux/linux-shell-printf.html)
