-1

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.

James Wierzba
  • 16,176
  • 14
  • 79
  • 120
  • pls explain downvote – James Wierzba Jan 31 '19 at 20:47
  • Shell expansion happens once. You can use a command `< <(printf "$1\n$2\n")` – KamilCuk Jan 31 '19 at 20:47
  • This has nothing whatsoever to do with herestrings; it's a question about how to expand variables in `$''`, which isn't herestring-specific syntax. You'd have the exact same issue with, f/e, `var=$'${INPUT1}\n${INPUT2}\n'` – Charles Duffy Jan 31 '19 at 20:47
  • @CharlesDuffy well I don't know the canonical name for `$''` in bash, what is a better name for the question? – James Wierzba Jan 31 '19 at 20:50
  • `$''` syntax is also known as [ANSI C-like strings](https://wiki.bash-hackers.org/syntax/quoting#ansi_c_like_strings), though that's admittedly quite a mouthful. Same thing happens with just regular single quotes, though; this could *almost* be generalized to [How do I use variables in single quoted strings?](https://stackoverflow.com/questions/21192420/how-do-i-use-variables-in-single-quoted-strings), which does cover the necessary details (re: being able to switch between quoting types in a single string). – Charles Duffy Jan 31 '19 at 20:51
  • @KamilCuk, better `< <(printf '%s\n' "$1" "$2")`, which won't try to treat values inside the variables as if they were part of the format string (with backslashes, `%` signs, etc non-literal). – Charles Duffy Jan 31 '19 at 22:29

1 Answers1

3

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.

Wiimm
  • 2,971
  • 1
  • 15
  • 25