diff --git a/git-submodule.sh b/git-submodule.sh
index 4fd8982894a..07dc675cef2 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -285,6 +285,10 @@ cmd_foreach()
 
 	toplevel=$(pwd)
 
+	# dup stdin so that it can be restored when running the external
+	# command in the subshell (and a recursive call to this function)
+	exec 3<&0
+
 	module_list |
 	while read mode sha1 stage path
 	do
@@ -301,7 +305,7 @@ cmd_foreach()
 				then
 					cmd_foreach "--recursive" "$@"
 				fi
-			) ||
+			) <&3 3<&- ||
 			die "Stopping at '$path'; script returned non-zero status."
 		fi
 	done
diff --git a/t/t7407-submodule-foreach.sh b/t/t7407-submodule-foreach.sh
index 97b7562dbfe..8a74ccac5f8 100755
--- a/t/t7407-submodule-foreach.sh
+++ b/t/t7407-submodule-foreach.sh
@@ -288,7 +288,7 @@ test_expect_success 'use "update --recursive nested1" to checkout all submodules
 	)
 '
 
-test_expect_failure 'command passed to foreach retains notion of stdin' '
+test_expect_success 'command passed to foreach retains notion of stdin' '
 	(
 		cd super &&
 		git submodule foreach echo success >../expected &&
@@ -297,7 +297,7 @@ test_expect_failure 'command passed to foreach retains notion of stdin' '
 	test_cmp expected actual
 '
 
-test_expect_failure 'command passed to foreach --recursive retains notion of stdin' '
+test_expect_success 'command passed to foreach --recursive retains notion of stdin' '
 	(
 		cd clone2 &&
 		git submodule foreach --recursive echo success >../expected &&