How to pass the value of a variable to the stdin of a command?

user283145 picture user283145 · Jan 23, 2011 · Viewed 86.3k times · Source

I'm writing a shell script that should be somewhat secure i.e. does not pass secure data through parameters of commands and preferably does not use temporary files. How can I pass a variable to the stdin of a command? Or, if it's not possible, how to correctly use temporary files for such task?

Answer

Martin picture Martin · Jan 23, 2011

Passing a value to stdin in bash is as simple as:

your-command <<< "$your_variable"

Always make sure you put quotes around variable expressions!

Be cautious, that this will probably work only in bash and will not work in sh.