David Pfau
David Pfau

Reputation: 813

Pipe data into shell command expecting a filename

Suppose I have a shell command foo that expects a file such as bar.txt as an argument, and I want to pass a one-line file to foo and then erase it like so:

echo "This is a one line file" > bar.txt
foo bar.txt
rm bar.txt

Is there a way to do this all in a single line of shell script without ever creating the file bar.txt?

Upvotes: 1

Views: 414

Answers (3)

sa77
sa77

Reputation: 3603

try command substitution like this

cat $(ls)

where, result of 'ls' will be substituted as an argument for 'cat' to execute

Upvotes: 0

PM 2Ring
PM 2Ring

Reputation: 55469

I'm assuming that foo doesn't try to read stdin. If so, as an alternative to the Process Substitution suggested by Cyrus, you can also do

 echo "This is a one line file" | foo /dev/stdin

Upvotes: 0

Cyrus
Cyrus

Reputation: 88543

You can use Process Substitution:

foo <(echo "This is a one line file")

Upvotes: 3

Related Questions