Bash – Create ‘Virtual File’ from Command Output

bashredirection

I wonder if there is a way to create a 'virtual file' from a bash output.

Example:
Let's say I want to email the output of mysqldump as an attachment to an external email address.
I can use Mutt to do so.
The mutt option I need to use is -a <name of the file I want to attach>.
I know I could use a temporary file:

mysqldump mysqldumpoptions > /tmp/tempfile && mutt -a /tmp/tempfile admin@example.org

But I would rather redirect the mysqldump output directly to Mutt instead.
Mutt's -a option only accepts a file and not a stream,
but maybe there is a way to pass it some kind of virtual file descriptor or something along those lines.
Something like:

mutt -a $(mysqldump mysqldumpoptions) admin@example.org

Is it possible? If not, why?

This is maybe a silly example and there surely are easier ways to do this, but I hope it explains my question about creating a virtual file from the output of another command.

Best Answer

This is the cleanest way to do what you want:

mutt admin@example.org -a <(mysqldump mysqldumpoptions)

The <() operator is what you were asking for; it creates a FIFO (or /dev/fd) and forks a process and connects stdout to the FIFO. >() does the same, except connects stdin to the FIFO instead. In other words, it does all the mknod stuff for you behind the scenes; or on a modern OS, does it in an even better way.

Except, of course, that doesn't work with mutt, it says:

/dev/fd/63: unable to attach file.

I suspect the problem is that mutt is trying to seek in the file, which you can't do on a pipe of any sort. The seeking is probably something like scanning the file to figure out what MIME type it is and what encodings might work (ie, whether the file is 7bit or 8bit), and then seeking to the beginning of the file to actually encode it into the message.

If what you want to send is plain text, you could always do something like this to make it the main contents of the email instead (not ideal, but it actually works):

mysqldump mysqldumpoptions | mutt -s "Here's that mysqldump" admin@example.org