Reading input while also piping a script via stdin

后端 未结 2 1941
隐瞒了意图╮
隐瞒了意图╮ 2021-01-22 05:14

I have a simple Bash script:

#!/usr/bin/env bash
read X
echo \"X=$X\"

When I execute it with ./myscript.sh it works. But when I ex

2条回答
  •  遇见更好的自我
    2021-01-22 06:15

    When you cat a script to bash the code to execute is coming from standard input.

    Where does read read from? That's right also standard input. This is why you can cat input to programs that take standard input (like sed, awk, etc.).

    So you are not running "a script" per-se when you do this. You are running a series of input lines.

    Where would you like read to read data from in this setup?

    You can manually do that (if you can define such a place). Alternatively you can stop running your script like this.

提交回复
热议问题