Bash can run scripts from pipeline. A minimal example would be:
> echo "echo hello world" | bash
hello world
This is handy if you wants to run a script downloaded from curl
or wget
.
But apparently, you cannot use read
in this kind of script execution. For example,
#!/bin/bash
echo 'input: '
read HELLO
echo hello: $HELLO
If you simply run the above script from a bash script file, you'd get to input the value of $HELLO
. and the echo hello: $HELLO
will print the result.
If you feed the script in the manner we talked about previously:
> echo "echo 'input: '; read HELLO; echo hello: $HELLO" | bash
input:
hello:
You won't get the interaction at all. And $HELLO
remains empty. I've also tried to add -i
option in bash, and it didn't work.
Is there any way we can use read
in the pipelined script? How? Or, if we can't, why?