Let's say I have a main.sh
script that will be calling one.sh
via a subshell.
one.sh
:
#! /bin/bash
set -euo pipefail
(if [ -t 0 ]; then
echo "one little two little three little buses"
else
cat -
fi) | awk '{ $1 = "111"; print $0 }'
main.sh
:
#! /bin/bash
set -euo pipefail
main() {
echo "one_result) $(./one.sh)"
echo "one_piped) $(echo "the quick brown fox" | ./one.sh)"
}
main
Now, each of them works as expected:
$ ./one.sh
111 little two little three little buses
$ ./main.sh
one_result) 111 little two little three little buses
one_piped) 111 quick brown fox
But, when I pipe something to main.sh
, I was not expecting (or, rather, I don't want) one.sh
to know about the piped content, because I thought one.sh
was in its own subshell in one_result)
:
$ echo "HELLO WORLD MAIN" | ./main.sh
one_result) 111 WORLD MAIN
one_piped) 111 quick brown fox
Is it the case my if
condition in one.sh
is not what I want? I would like one.sh
to not create any side-effects of consuming my main.sh
's stdin - since now it has consumed it, my main.sh
is now effectively stdin
-less, as stdin can only be read once unless I store it away.
Thoughts? TIA.