diff --git a/src/lib/parser/shell_expand/mod.rs b/src/lib/parser/shell_expand/mod.rs
index 6c13a550d26354f868f49e25dbafc37a0b8d4678..df93ff2340abb1fd12781395da9a17aca2bdf75c 100644
--- a/src/lib/parser/shell_expand/mod.rs
+++ b/src/lib/parser/shell_expand/mod.rs
@@ -559,13 +559,37 @@ fn expand<E: Expander>(
     do_glob: bool,
     tilde: bool,
 ) {
+    let concat: small::String = match output.rfind(char::is_whitespace) {
+        Some(sep) => {
+            if sep != output.len() - 1 {
+                let word_start = sep + 1;
+                let mut t: small::String = output.split_at(word_start).1.into();
+                t.push_str(text);
+                output.truncate(word_start);
+                t
+            } else {
+                text.into()
+            }
+        }
+        None => {
+            if output.is_empty() {
+                text.into()
+            } else {
+                let mut t = output.clone();
+                t.push_str(text);
+                output.clear();
+                t
+            }
+        }
+    };
+
     let expanded: small::String = if tilde {
-        match expand_func.tilde(text) {
+        match expand_func.tilde(&concat) {
             Some(s) => s.into(),
-            None => text.into(),
+            None => concat,
         }
     } else {
-        text.into()
+        concat
     };
 
     if do_glob {