diff --git a/src/lib/shell/shell_expand.rs b/src/lib/shell/shell_expand.rs
index 606ca7a45cd6b80068ac3c809bce6633304493dd..dcd923ba2531bb1397f211ca3b57052dbb88971b 100644
--- a/src/lib/shell/shell_expand.rs
+++ b/src/lib/shell/shell_expand.rs
@@ -29,21 +29,22 @@ impl<'a, 'b> Expander for Shell<'b> {
         let prev_stderr = self.stderr(null_file);
 
         // Execute the command
-        self.on_command(command).map_err(|err| Error::Subprocess(Box::new(err)))?;
+        let result = self.on_command(command).map_err(|err| Error::Subprocess(Box::new(err)));
 
         // Reset the pipes, droping the stdout
         self.stdout(prev_stdout);
         self.stderr(prev_stderr);
 
+        // Ensure that the parent retains ownership of the terminal before exiting.
+        let _ = tcsetpgrp(nix::libc::STDIN_FILENO, Pid::this());
+
+        result?;
+
         let mut string = String::with_capacity(1024);
-        let output = match reader.read_to_string(&mut string) {
+        match reader.read_to_string(&mut string) {
             Ok(_) => Ok(string.into()),
             Err(why) => Err(Error::Subprocess(Box::new(PipelineError::CaptureFailed(why).into()))),
-        };
-
-        // Ensure that the parent retains ownership of the terminal before exiting.
-        let _ = tcsetpgrp(nix::libc::STDIN_FILENO, Pid::this());
-        output
+        }
     }
 
     /// Expand a string variable given if its quoted / unquoted