echolocation
echolocation

Reputation: 1130

In bash or shell, how does one input a line of text as a file without making a temp file?

Problem 1:

I'm using a self-made command, that for reasons, cannot be changed. The command requires a filename that it will read from like so: read-cmd "testtext.txt" I know it's possible to use files as a stream of input for some commands using input redirection, e.g. some-cmd < "text.txt", but I'm wondering if the opposite is true, whether I can use a line of text and make bash believe it's a file, so that I'd be able to do read-cmd "contents of what should be in a text file"

the only thing I've been able to do is

Example 1:
echo "contents of what should be in a text file" > tmptextfile
read-cmd "tmptextfile"
rm tmptextfile

I would however, really rather not do this, and rather just stream that line in as if it were a file. Is there any possible way to do this, or would it rely entirely on how read-cmd works?

Problem 2:

A very similar issue, however, instead of the file being an input to a command, it is the input to an option of a command. So, read-cmd2 -d "testtext.txt" ...

Example 2:
echo "contents of what should be in options text file" > tmpoptfile
read-cmd2 -d tmpoptfile ...
rm tmpoptfile

Upvotes: 3

Views: 791

Answers (1)

anubhava
anubhava

Reputation: 785611

whether I can use a line of text and make bash believe it's a file,

Yes you can use process substitution for this:

read-cmd <(echo "contents of what should be in a text file")

Process substitution is a form of redirection where the input or output of a process (some sequence of commands) appear as a temporary file.

Upvotes: 5

Related Questions