Index: uspace/app/bdsh/input.c
===================================================================
--- uspace/app/bdsh/input.c	(revision d6a982489f5ac8b795d611169cf936ce6d9a1058)
+++ uspace/app/bdsh/input.c	(revision b64073879eb2e573f0761f49da42da1b6da8214c)
@@ -200,5 +200,5 @@
 			pipe_pos[pipe_count++] = i;
 			cmd_argc = i;
-			redir_to = tmpnam(NULL);
+			redir_from = redir_to = tmpnam(NULL);
 			break;
 
@@ -251,23 +251,4 @@
 	FILE *to = NULL;
 
-	if (redir_from) {
-		from = fopen(redir_from, "r");
-		if (from == NULL) {
-			printf("Cannot open file %s\n", redir_from);
-			rc = errno;
-			goto finit_with_files;
-		}
-		new_iostate.stdin = from;
-	}
-
-	if (redir_to) {
-		to = fopen(redir_to, "w");
-		if (to == NULL) {
-			printf("Cannot open file %s\n", redir_to);
-			rc = errno;
-			goto finit_with_files;
-		}
-		new_iostate.stdout = to;
-	}
 
 	for (unsigned p = 0; p <= pipe_count; p++) {
@@ -302,10 +283,30 @@
 		}
 
-		if (p < pipe_count) {
-			new_iostate.stdout = to;
-		}
-		if (p && p == pipe_count) {
-			fseek(to, 0, SEEK_SET);
-			new_iostate.stdin = to;
+		if (redir_to) {
+			if ((p < pipe_count) || (pipe_count == 0)) {
+				to = fopen(redir_to, "w");
+				if (to == NULL) {
+					printf(
+					    "Cannot open file %s redirect to\n",
+					    redir_to);
+					rc = errno;
+					goto finit_with_files;
+				}
+				new_iostate.stdout = to;
+			}
+		}
+
+		if (redir_from) {
+			if ((p && p == pipe_count) || (pipe_count == 0)) {
+				from = fopen(redir_from, "r");
+				if (from == NULL) {
+					printf("Cannot open file %s redirect "
+					       "from\n",
+					    redir_from);
+					rc = errno;
+					goto finit_with_files;
+				}
+				new_iostate.stdin = from;
+			}
 		}
 
@@ -316,4 +317,12 @@
 		}
 
+		if (to) {
+			fclose(to);
+			to = NULL;
+		}
+		if (from) {
+			fclose(from);
+			from = NULL;
+		}
 		// Restore the Standard Input, Output and Error file descriptors
 		new_iostate.stdin = stdin;
