Search code examples
bashstdinheredocherestring

How to expand env var in ANSI C-like string and use literal newline?


I am using herestring to pass a string (two input values with newlines) as standard input to an installer executable. For example, executing an installer with two inputs /var/tmp and yes

#!/bin/bash
# run_installer.sh

./installer <<< $'/var/tmp\nyes\n'

But, I need to parameterize the inputs.

e.g.

#!/bin/bash
# run_installer.sh
export INPUT1="$1"
export INPUT2="$2"

# does not work, it evaluates literally to: ./installer ${INPUT1} ${INPUT2}
./installer <<< $'${INPUT1}\n${INPUT2}\n'

So that I can execute it like so:

./run_installer /var/tmp yes

The question that was marked as a duplicate does not answer this question. It is similar in concept, but different enough to warrant it's own question.


Solution

  • Try:

    ./installer <<< "${INPUT1}"$'\n'"${INPUT2}"$'\n'
    

    or:

    EOL=$'\n'
    ./installer <<< "${INPUT1}${EOL}${INPUT2}${EOL}"
    

    Anyway, the last EOL ist not needed, because it is inserted automatically.