0

I read How do I set a variable to the output of a command in Bash? and tried to do that twice:

TESTVAR=$(echo 1) $(echo 2)`
TESTVAR="$(echo 1)" "$(echo 2)"
TESTVAR=`echo 1` `echo 2`

All three options fail with -bash: 2: command not found.

Bash version 4.3.30

MSalters
  • 173,980
  • 10
  • 155
  • 350

1 Answers1

0

The problem is that bash runs the two commands, captures their outputs, and then substitutes that to produce TESTVAR=1 2. That assigns 1 to TESTVAR and tries to run 2. Apparently, there's no executable called "2" on my PATH.

The second attempt with quotes came close, but the quotes don't belong to the individual substiturions. You need a single pair:

TESTVAR="$(echo 1) $(echo 2)"
MSalters
  • 173,980
  • 10
  • 155
  • 350