bashzsh

How to exit a shell script when a subshell fails and also using read


Following on this post

I have this bash script that I would expect to terminate entirely upon hitting the false and yet, it doesn't. I recognize I am doing things slightly different that the linked post.

#!/usr/bin/env bash
set -Eeuo pipefail

function thing() {
  false
  echo "thing1|thing2|thing3"
}
IFS='|' read -r one two three <<< "$(thing )"
echo "${one} ${two} ${three}"
echo "done"

Output:

thing1 thing2 thing3
done

Ideally I'd like to do this in zsh but I also get behavior I don't want:


#!/usr/bin/env zsh
set -Eeuo pipefail

function thing() {
  false
  echo "thing1|thing2|thing3"
}
IFS='|' read -r one two three <<< "$(thing )"
echo "${one} ${two} ${three}"
echo "done"

output:

  
done

How to make this work?


Solution

  • You can do

    IFS='|' read -r one two three < <(thing)
    

    but checking for an error explicitly as William suggests in his answer is better.