using a Bash variable in place of a file as input for an executable

后端 未结 4 1631
误落风尘
误落风尘 2020-12-15 04:35

I have an executable that is used in a way such as the following:

executable -v -i inputFile.txt -o outputFile.eps

In order to be more effi

4条回答
  •  我在风中等你
    2020-12-15 05:34

    Echo is not safe to use for arbitrary input.

    To correctly handle pathological cases like inputdata='\ntest' or inputdata='-e', you need

    executable -v -i <(cat <<< "$inputData")
    

    In zsh, the cat is not necessary


    Edit: even this adds a trailing newline. To output the exact variable contents byte-by-byte, you need

    executable -v -i <(printf "%s" "$inputData")
    

提交回复
热议问题