How does Bash handle commands read from standard input?
From:
Cristian
Subject:
How does Bash handle commands read from standard input?
Date:
Tue, 13 Oct 2026 16:09:36 +0200
User-agent:
Mozilla Thunderbird
Hello all,
In the current directory I have the following file
---- begin file.txt ----
Hello
---- end file.txt ----
Case 1:
In an interactive shell running in a console, or in an X terminal (in my case
xfce4-terminal), when I type "cat file.txt" I see the text "cat file.txt"
at the command prompt (nothing strange).
Case 2:
In an interactive shell running in a console, or in an X terminal (in my case
xfce4-terminal), if I run the command in the line 1, the output is as shown
on lines 2–4.
1 | $ echo "cat file.txt" | bash -i
2 | $ cat file.txt
3 | Hello
4 | $
Line 2 is the command read by bash -i ("echo Hello") from its stdin. It
is obvious that the bash process started by "bash -i" prints it.
I have some questions:
1. Which process prints it in the case 1?
2. Which process prints it in the case 2?
3. Is the shell started in Case 2 a “true” interactive shell,
even though its standard input is not connected to the
terminal? In other words, does it have all the characteristics
of a shell running interactively in an X terminal?
4. Is it possible to prevent the command read from standard input (the
string "cat file.txt") from being printed to the terminal?
Cheers,
Chris