diff --git a/src/lib/shell/mod.rs b/src/lib/shell/mod.rs
index 326f8ce33107271527da01cdeb7e2c0f930b57c9..3192ee9ebdca5f5b412b1ec805d4923b4df9a556 100644
--- a/src/lib/shell/mod.rs
+++ b/src/lib/shell/mod.rs
@@ -434,7 +434,9 @@ impl<'a> Expander for Shell {
     /// Expand a string variable given if its quoted / unquoted
     fn string(&self, name: &str, quoted: bool) -> Option<types::Str> {
         use ascii_helpers::AsciiReplace;
-        if quoted {
+        if name == "?" {
+            Some(types::Str::from(self.previous_status.to_string()))
+        } else if quoted {
             self.get::<types::Str>(name)
         } else {
             self.get::<types::Str>(name)
diff --git a/src/lib/shell/variables/mod.rs b/src/lib/shell/variables/mod.rs
index 3f241562a74e53a2cbee7e2213d7f7086048d750..108a50b1ed257e34797f20b893799afb8c3e54e9 100644
--- a/src/lib/shell/variables/mod.rs
+++ b/src/lib/shell/variables/mod.rs
@@ -318,10 +318,6 @@ impl Variables {
         const GLOBAL_NS: &str = "global::";
         const SUPER_NS: &str = "super::";
 
-        if name == "?" {
-            return self.scopes().last()?.get("?");
-        }
-
         let mut up_namespace: isize = if name.starts_with(GLOBAL_NS) {
             name = &name[GLOBAL_NS.len()..];
             // Go up as many namespaces as possible