Index: /trunk/src/kash/expand.c
===================================================================
--- /trunk/src/kash/expand.c	(revision 2305)
+++ /trunk/src/kash/expand.c	(revision 2306)
@@ -419,4 +419,7 @@
 	int saveherefd;
 	int quotes = flag & (EXP_FULL | EXP_CASE);
+#ifdef SH_DEAL_WITH_CRLF
+	int pending_cr = 0;
+#endif
 
 	INTOFF;
@@ -448,4 +451,17 @@
 		}
 		lastc = *p++;
+#ifdef SH_DEAL_WITH_CRLF
+		if (pending_cr) {
+			pending_cr = 0;
+			if (lastc != '\n') {
+				if (quotes && syntax[(int)'\r'] == CCTL)
+					STPUTC(psh, CTLESC, dest);
+				STPUTC(psh, '\r', dest);
+			}
+		}
+		if (lastc == '\r')
+			pending_cr = '\r';
+		else
+#endif
 		if (lastc != '\0') {
 			if (quotes && syntax[(int)lastc] == CCTL)
@@ -454,14 +470,16 @@
 		}
 	}
+#ifdef SH_DEAL_WITH_CRLF
+	if (pending_cr) {
+		if (quotes && syntax[(int)'\r'] == CCTL)
+			STPUTC(psh, CTLESC, dest);
+		STPUTC(psh, '\r', dest);
+	}
+#endif
 
 	/* Eat all trailing newlines */
 	p = stackblock(psh) + startloc;
-	while (dest > p && dest[-1] == '\n') {
+	while (dest > p && dest[-1] == '\n')
 		STUNPUTC(psh, dest);
-#ifdef SH_DEAL_WITH_CRLF
-		if (dest > p && dest[-1] == '\r')
-			STUNPUTC(psh, dest);
-#endif
-	}
 
 	if (in.fd >= 0)
