Search code examples
macosbashscript-fu

Pass a line and file contents into a program as stdin


I'm trying to pass in a file and a single line of code into a program, say example.

My line:

i := 1;

My file (file):

blah1
blah2
blah3

Input to program:

i := 1;
blah1
blah2
blah3

I'm thinking it would be a one line such as:

example < `echo "i := 1;\n" cat file`

or something like that


Solution

  • What you need is here string:

    example <<< `echo "i:=1" && cat file`
    

    From bash manual:

    3.6.7 Here Strings
    
    A variant of here documents, the format is:
    
         <<< word
    
    The word is expanded and supplied to the command on its standard input.