Christopher Shroba
Christopher Shroba

Reputation: 7554

How can I print contents of file given filename as stdin in bash?

Is there a bash command like cat except that it takes the filename from stdin rather than the first argument? For example:

echo "/home/root/file.txt" | somecommand

would have the same output as cat /home/root/file.txt

Upvotes: 1

Views: 240

Answers (2)

Christopher Shroba
Christopher Shroba

Reputation: 7554

Found the answer:

xargs cat

For example:

echo "/home/root/file.txt" | xargs cat

Upvotes: 1

holygeek
holygeek

Reputation: 16185

If the filename that you're expecting doesn't have a space:

#!/bin/bash    
read filename
cat $filename

The read builtin shell command reads tokens from stdin into environment variables.

Upvotes: 0

Related Questions