creating a temporary file in memory and using it as input file of a command

You can use process substitution for this:

pdflatex <(echo "$stuff")

From the Bash Reference Manual:

3.5.6 Process Substitution

Process substitution is supported on systems that support named pipes (FIFOs) or the /dev/fd method of naming open files. It takes the form of

<(list)

or

>(list)

The process list is run with its input or output connected to a FIFO or some file in /dev/fd. The name of this file is passed as an argument to the current command as the result of the expansion. If the >(list) form is used, writing to the file will provide input for list. If the <(list) form is used, the file passed as an argument should be read to obtain the output of list. Note that no space may appear between the < or > and the left parenthesis, otherwise the construct would be interpreted as a redirection.

And I also wonder if a here-string would make it as well:

pdflatex <<< "$stuff"

Another solution is to use a ramdisk, a filesystem that lives in ram. I always compile my LaTeX on a ramdisk to save disk writes, and also use one for Portage software builds on Gentoo for the same reason.

Here's how you create one:

Linux: sudo mkdir /mnt/ramdisk sudo mount -t tmpfs -o size=2G ramdisk /mnt/ramdisk

feel free to change the mount point to anything. a popular option is /tmp/tmpfs or /tmp/ram

macOS: diskutil erasevolume HFS+ "ramdisk" `hdiutil attach -nomount ram://$((sizeInGB*1024*2048))`

this example will mount on /Volumes/ramdisk

if you wish to use MB replace the end with ram://$((sizeInMB*2048))


Many shells, and Linux as a whole, accept:

echo "${stuff}"  | pdflatex /dev/stdin

None of the presented solutions is guaranteed to work on all possible commands.

Consider myCommand below (instead of pdflatex):

#!/bin/bash
# myCommand
test -f "$1" || { echo "error: Please provide a file"; exit 1; }
echo "The file content is:"$(cat $1)

Trying the existing solutions:

./myCommand <(echo "abcdef")
# error: Please provide a file

echo "abcdef" | ./myCommand /dev/stdin
# error: Please provide a file

In my Ubuntu I use the following approach (utilizing /dev/shm in-memory file storage):

ramtmp="$(mktemp -p /dev/shm/)"
echo "abcdef" > $ramtmp
./myCommand $ramtmp
# The file content is:abcdef

So in your case it would be:

ramtmp="$(mktemp -p /dev/shm/)"
for stuff in list of stuff; do
  echo "${stuff}" > $ramtmp
  pdflatex $ramtmp
done 

In case pdflatex execution is non-blocking add the first line inside the loop.

Note that, I'm not sure how many distributions currently support it. See Wikipedia article:

Linux distributions based on the 2.6 kernel and later offer /dev/shm as shared memory in the form of a RAM disk, more specifically as a world-writable directory (a directory in which every user of the system can create files) that is stored in memory. Both the RedHat and Debian based distributions include it by default. Support for this type of RAM disk is completely optional within the kernel configuration file.[5]

Tags:

Bash