Consider this script:
#!/bin/bash
test() {
local x='a';
echo 'b' | {
read x;
echo "x (should be 'b'): ${x}"
}
echo "x (should be 'b'): ${x}";
}
test
As you can see, x is initialized to 'a' and then within the block it's set to 'b' as that is what's piped to the block. After the block, the value of x is still 'a' rather than the expected 'b'. What's missing here?
Note: this is a simple reproduction, in reality this is used to gather the response and status code from a curl command, e.g.:
curl --write-out '\n%{http_code}' ... | {
read responseBody;
read responseCode;
}
# use ${responseBody} and ${responseCode} here...
(It's intended for use in testing where the response body is either empty or a single line.)