Shell 读取输入用 read

  • 简单的输入示例:read.sh

    1
    2
    3
    4
    #!/bin/bash

    read name
    echo "welcome $name"

    执行结果:

    1
    2
    3
    $ ./read.sh
    lizs // shell 脚本会等待输入变量值
    wecome lizs
  • 带提示的输入,可以在前面 echo 一句话,或者用 -p 来实现:

    1
    2
    3
    #!/bin/sh
    read -p "please input your name: " name
    echo "welcome $name"

    执行效果和上面一样。

  • 输入多个变量,

    1
    2
    3
    #!/bin/sh
    read -p "please input your name: " firstname lastname
    echo "welcome lastname"

    执行结果:

    1
    2
    3
    $ ./read.sh
    please input your name: hello world
    welcome hello world