How to pass a string to a command (Linux)

Posted on

How to pass a string to a command (Linux) – Managing your servers can streamline the performance of your team by allowing them to complete complex tasks faster. Plus, it can enable them to detect problems early on before they get out of hand and compromise your business. As a result, the risk of experiencing operational setbacks is drastically lower.

But the only way to make the most of your server management is to perform it correctly. And to help you do so, this article will share nine tips on improving your server management and fix some problem about linux, command, , , .

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!

Solution :

Just do

echo "my file content as a string" | my_command

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

grep tak <<<"foo bar tak"


foo="bar tak"
grep tak <<<$foo

echo “my file content as a string” | my_command

You can simply type :

my_command "my file content as a string"

Leave a Reply

Your email address will not be published.