Re: How does Bash handle commands read from standard input?
From:
Martin D Kealey
Subject:
Re: How does Bash handle commands read from standard input?
Date:
Wed, 14 Oct 2026 03:37:06 +1300
On Wed, 14 Oct 2026, 03:09 Cristian, <crstml@libero.it> wrote:
> 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?
>
when readline is enabled, Bash.
when readline is disabled, the kernel (cf "stty echo")
2. Which process prints it in the case 2?
>
bash, but only if readline is enabled; otherwise no command input is shown
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?
>
Mostly, yes, but there are limitation; especially, you don't get tty-driven
job control.
4. Is it possible to prevent the command read from standard input
> (the string "cat file.txt") from being printed to the terminal?
>
Omit the '-i' option when starting bash.
Cheers,
> Chris
>