Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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!

share|improve this question

4 Answers 4

up vote 8 down vote accepted

Just do

echo "my file content as a string" | my_command
share|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

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
share|improve this answer

echo "my file content as a string" | my_command

share|improve this answer

You can simply type :

my_command "my file content as a string"
share|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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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