I have a case where I am passing a content of a file to a command like this:

cat file_name.txt | my_command

But I want to pass a string directly, without reading a txt file content. So I want to have something like this:

"my file content as a string" | my_command

But of course I am getting an error "Command not found."

Please help me to come up with this. Thanks!

link|improve this question

50% accept rate
feedback

4 Answers

up vote 8 down vote accepted

Just do

echo "my file content as a string" | my_command
link|improve this answer
yeees, sure :)!!! – Narek Sep 14 '09 at 12:22
No, no, no my question was very simple but I have casually missed that “echo” will work fine!, please roll back to your prev answer. – Narek Sep 14 '09 at 12:26
2  
This is a wrong answer. echo "foo" will actually pass "foo\n". It has to be echo -n "foo". – Anonymous Sep 14 '09 at 13:34
That depends on what your my_command is expecting – wzzrd Sep 14 '09 at 13:35
feedback

Echo is fine, with bash you can also use a here string if you want. For example:

grep tak <<<"foo bar tak"

Or:

foo="bar tak"
grep tak <<<$foo
link|improve this answer
feedback

echo "my file content as a string" | my_command

link|improve this answer
feedback

You can simply type :

my_command "my file content as a string"
link|improve this answer
1  
Whether that would work, depends heavily on the command used – wzzrd Sep 14 '09 at 12:24
in my case that is not a solution, sorry – Narek Sep 14 '09 at 12:37
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.