Skip to content

Instantly share code, notes, and snippets.

@jpf
Created May 6, 2009 00:20
Show Gist options
  • Select an option

  • Save jpf/107298 to your computer and use it in GitHub Desktop.

Select an option

Save jpf/107298 to your computer and use it in GitHub Desktop.

Revisions

  1. jpf revised this gist May 6, 2009. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    #!/bin/bash
    # How to exit if any command in a sub-shell fails.
    # How to exit if any command in a bash subshell fails.

    exitIfFailed ()
    {
  2. jpf created this gist May 6, 2009.
    30 changes: 30 additions & 0 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    #!/bin/bash
    # How to exit if any command in a sub-shell fails.

    exitIfFailed ()
    {
    rv=$?;
    if [ $rv -ne 0 ]; then
    exit $rv;
    fi
    }


    (
    echo -n "T";
    # false;
    exitIfFailed;
    echo -n "e";
    # false;
    exitIfFailed;
    echo -n "s";
    # false;
    exitIfFailed;
    echo -n "t";
    # false;
    exitIfFailed;
    ) # > /dev/null 2>&1
    subshell_rv=$?
    echo ""

    echo "Returned: $subshell_rv"