Just to say it up front: I am aware that most programs that have an interactive prompt also have some kind of initialization file or the ability to set a command flag that will run something, and then drop you into the interactive prompt. However, I'm interested in the solution at the shell level.

Let's say I have a command xyz that I run from a shell, and it takes over my terminal and gives me an interactive prompt where I can run newline terminated commands.

Let's say I have two commands (two lines) I want to run at the beginning of my interactive session, each and every time I run xyz.

If these two lines were the only commands I wanted to run, of course I could just run:

printf '%s\n' 'first command' 'second command' | xyz

Or alternatively, if I had a whole bunch of commands to run but I know all of them in advance, I could just put them all in a file and run:

xyz < commands-file.txt

However, if I want to get a standard interactive prompt, with the exact same result as if I typed xyz and then manually typed in my first command and second command...is there a way to do this in the shell (e.g. in a shell function) using redirection?

The obvious apparent solutions such as sed -e '1ifirst command' -e '1isecond command' | xyz don't work, either because xyz can detect that the input is not coming from a terminal or because the sed output is buffered; not sure which.

  • take a look at qodem – Serge Sep 20 '16 at 9:32
  • 1
    expect.sourceforge.net – Ipor Sircer Sep 20 '16 at 9:35
  • @IporSircer, no, I know about expect. That isn't relevant. I've bolded the applicable line to make it more obvious. – Wildcard Sep 20 '16 at 9:37
  • @Serge, I hadn't heard of qodem but a quick search and browse of several of the results doesn't reveal the relevance to my question. Care to clarify? – Wildcard Sep 20 '16 at 9:38
  • 1
    You want to feed something into an xyz programs stdin prior you type something else manually. The simple shell redirection would not help much (even cat file - | xyz would not help as xyz would detect that stdin is not a tty and cat does buffering) . You need some terminal emulation program that 1) is capable to initially send something to the sub-process and 2) is not buffering the input and 3) looks as a tty to the subprocess – Serge Sep 20 '16 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.