I wrote a program in Fortran that asks me for latitude and longitude and the name of the output file every time I run it. It works great. What I have been meaning to do is run this program for the entire globe (i.e. 65341 total different possible combination). I am having a hard time doing this with shell scripting. Can any one give me some pointers? So far I have been messing around with an input file, but the program reads the first three entries of the input file (lat, lon, and the filename) and stops. What would be excellent is if I could write a source script that goes something like the following:

$ ./myprogram < lat1, lon1, filename1

$ ./myprogram < lat2, lon2, filename2

However, the sign < only feeds in the input file. Is there a similar way to feed in input values instead of input file?

  • You might be able to pipe the values into the program using echo: echo values | ./myprogram.
    – HalosGhost
    Commented Jul 31, 2014 at 2:21
  • Thank you. I already tried that.The problem is all the values appearing in one line. This is the error I get: Latitude of feature of interest (degrees) > Longitude (degrees) > At line 87 of file LocalizedAdmitCorr.f95 (unit = 5, file = 'stdin') The way to make this work is if each of the components (i.e. lat lon and filename) appear in a new line. The way it is right now "echo lat lon filename", the program assumes all of these things are input lat.
    – Luju
    Commented Jul 31, 2014 at 2:24

2 Answers 2


Bash supports here documents and here strings to provide standard input to a program from a string. The following:

./myprogram <<EOT

will give myprogram three lines of input, which are the values of the lat1, lon1, and filename1 variables. If you put that inside a loop that sets the variables appropriately each time around then you will run myprogram many times, with different standard input every time.

<<EOT marks a here document: it means to read the following lines until one just says "EOT" and to provide that as standard input to the program (like < does for a file). You can use whatever you like in place of EOT, so long as it's the same in both places.

You can also use a here string:

./myprogram <<<"$lat

This does the same thing, but with a multi-line string literal instead. <<<"..." takes the given string and passes it in as standard input just like above. I would prefer the heredoc version, though.

An alternative approach that works even in POSIX sh is just to create the appropriate file:

echo $lat > file
echo $lon >> file
echo $filename >> file
./myprogram < file

The here document is better if you can use it, though, which I assume you can since this is tagged "bash".

A still further option uses \n escapes and either echo or printf:

printf '%s\n%s\n%s\n' "$lat" "$lon" "$filename" | ./myprogram
echo -e "$lat\n$lon\n$filename" | ./myprogram

-e is a common extension to echo that Bash and other shells support. printf should work on any POSIX-compatible system.


The easy (and portable) way to feed multiple lines of input to a program is

  echo 'lat1'
  echo 'lon1'
  echo 'filename1'
} | ./myprogram

An alternative approach is to use a here document.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .