This might work:
(echo "something"; cat -) | ./my_program
It creates a sub-shell where the first line of output comes from echo
and the rest comes from the standard input to cat
, which is the terminal (or the script's standard input, at any rate). I use the -
to emphasize that cat
is required to read from standard input — it is not simply that I've forgotten to specify "$@"
or something after the cat
command. Omitting the -
doesn't make an operational difference; it might make a comprehensibility difference.
Be aware that the input to my_program
is no longer a terminal but a pipe connected to the terminal, which can affect the behaviour of the program. The cat
process introduces delays, too.
Expect
If that doesn't do the job, then you probably need to use expect
instead. This is a general purpose tool for scripting interactions with other programs, and it uses pseudo-ttys (ptys) to make it appear as if a user at a terminal is communicating with the other program.
As anishsane notes, expect
has an interact
command that can be used to leave you with the user typing to the program after some fixed preamble.
Beware
Adapting this to your second scenario was not a comfortable experience:
(echo date; cat -) | bash -i
The -i
tells Bash it is an interactive shell. The date
worked, and I got a prompt after it, but I didn't get any more commands executed. Interrupt got me more prompts; pretty much everything else seemed to be ignored. That may be cat
doing too much buffering of its output.
I ended up killing that shell from another terminal window. I had better luck with:
(echo date; cat -) | bash
but there were no prompts from Bash. Be careful; make sure you know how to get out of trouble.
I/O Redirection Stunts
rici also pointed out that in this particular case, you could use:
{ { echo date; echo 'exec 0<&3-';} | bash -i; } 3<&0
That's rather clever because the trailing 3<&0
makes a copy of the original standard input (file descriptor 0) on descriptor 3, and then runs bash -i
with its input coming from two echo
statements. The first requests the date. The second re-redirects things so that the standard input now comes from file descriptor 3 (that's the 0<&3
part) — which is the original standard input, aka 'the terminal' — and closes file descriptor 3 too (that's the trailing -
; it is a Bash extension over POSIX shell I/O redirection).