1

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!

8

Just do

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

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
1

echo "my file content as a string" | my_command

0

You can simply type :

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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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