shell

Exit shell from a subshell


I believe I am calling exit in a subshell that causes my program to continue:

#!/bin/bash
grep str file | while read line
do
        exit 0
done
echo "String that should not really show up!"

Any idea how I can get out of the main program?


Solution

  • You can trivially restructure to avoid the subshell -- or, rather, to run the grep inside the subshell rather than the while read loop.

    #!/bin/bash
    while read line; do
      exit 1
    done < <(grep str file)
    

    Note that <() is bash-only syntax, and does not work with /bin/sh.