diff --git a/src/header/stdlib/mod.rs b/src/header/stdlib/mod.rs
index bf290f693deaa5d81d473afbfbfebc3ead5bec3c..7ce198255975fc1711fb7a37101c3c4023cbbef1 100644
--- a/src/header/stdlib/mod.rs
+++ b/src/header/stdlib/mod.rs
@@ -1170,13 +1170,19 @@ pub unsafe extern "C" fn strtoll(
 pub unsafe extern "C" fn system(command: *const c_char) -> c_int {
     //TODO: share code with popen
 
+    // handle shell detection on command == NULL
+    if command.is_null() {
+        let status = system("exit 0\0".as_ptr() as *const c_char);
+        if status == 0 {
+            return 1;
+        } else {
+            return 0;
+        }
+    }
+
     let child_pid = unistd::fork();
     if child_pid == 0 {
-        let command_nonnull = if command.is_null() {
-            "exit 0\0".as_ptr()
-        } else {
-            command as *const u8
-        };
+        let command_nonnull = command as *const u8;
 
         let shell = "/bin/sh\0".as_ptr();
 
diff --git a/tests/expected/bins_static/stdlib/system.stdout b/tests/expected/bins_static/stdlib/system.stdout
index a6d3f18a28c03179fddd3a8bd481ead790de30cc..90f99da2e10bcfc715c0e0cd87433368437d5c6a 100644
--- a/tests/expected/bins_static/stdlib/system.stdout
+++ b/tests/expected/bins_static/stdlib/system.stdout
@@ -1 +1,2 @@
+shell found: 1
 test of system
diff --git a/tests/stdlib/system.c b/tests/stdlib/system.c
index 8b7d02201aca1b94dc249866d4a5149ed917c113..821c216f6c530c955ff3dfa6d1fd16922c40aace 100644
--- a/tests/stdlib/system.c
+++ b/tests/stdlib/system.c
@@ -3,6 +3,16 @@
 #include "test_helpers.h"
 
 int main(void) {
-    int status = system("echo test of system");
+    // testing shell detection
+    // this means, because we don't detect if a shell actually exists but just assume it does, this test case breaks in
+    // environments without `sh`. I think that is a reasonable tradeoff.
+    // (And if there isn't a shell, system() won't work anyways)
+    int status = system(NULL);
+    printf("shell found: %i\n", status);
+    fflush(stdout);
+    ERROR_IF(system, status, == 0);
+
+    // base case
+    status = system("echo test of system");
     ERROR_IF(system, status, == -1);
 }