The issue in your Bash script is due to how pipes (|) and variable scoping work in Bash.
echo hey | read test: This line pipes the output of echo hey to the read test command. However, in Bash, each command in a pipeline runs in its own subshell. This means the read test command executes in a subshell, and the variable test is only set within that subshell.echo $test: This line runs in the parent shell, where the variable test was never set (because it was set in a subshell). Thus, $test is empty, and nothing is printed.test is local to the subshell created by the pipeline and is not available in the parent shell where echo $test runs.If you want to capture the output of echo hey into the variable test and use it later, avoid using a pipe with read. Instead, use command substitution:
bashtest=$(echo hey) echo $test
This will correctly set test to hey and print it.
Alternatively, if you want to use read, you can do it like this:
bashecho hey | { read test; echo $test; }
But this still only works within the same subshell. To use test later, you need to avoid the subshell, as in the first example.
Vibe can make mistakes. Check answers. Learn more