Zsh – Specifying the File Extension Produced by Process Substitution

process-substitutionshellzsh

I want to use zsh's process substitution to create a temporary file that can then be read by another program. However, the file it generates has no file extension, so the program reading it refuses to continue.

This can be demonstrated by:

$ echo =(ls)
/tmp/zshmgIWvT

What I want to happen is it to produce a filename like /tmp/zshmgIWvT.wav

Can zsh do this?

Best Answer

Somewhere in the depths of zshparam(1) one may find:

   TMPSUFFIX
          A  filename  suffix which the shell will use for temporary files
          created by process substitutions (e.g., `=(list)').   Note  that
          the  value  should  include  a leading dot `.' if intended to be
          interpreted as a file extension.  The default is not  to  append
          any  suffix,  thus  this  parameter should be assigned only when
          needed and then unset again.

So

% () { TMPSUFFIX=.wav; print =(ls); unset TMPSUFFIX }
/Users/jhqdoe/tmp/zshsQhnnV.wav
% print $TMPSUFFIX

% 

You may also want to set TMPPREFIX especially on shared systems to help avoid various /tmp security flaws.