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

后端 未结 9 581
無奈伤痛
無奈伤痛 2020-11-27 14:44

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

9条回答
  •  猫巷女王i
    2020-11-27 15:06

    Just do:

    printf "$my_var" | my_cmd
    

    If the var doesn't contain spaces then the quotes may be omitted.
    If using bash then you may also do:

    echo -n "$my_var" | my_cmd
    

    Avoid using echo without -n because it will pipe the vraiable with an added linebreak at the end.

提交回复
热议问题