0

I have written the following command substitution and executed in in a bash shell:

$(echo echo 1; echo 2; echo 3) #output: 1 2 3

Why is the double echo required in the first expression, while only single echos are required in the second and third?

Display name
  • 721
  • 2
  • 11
  • 29

1 Answers1

1

It has to do with what you've actually asked the shell to do. That is as follows:

"Echo the result of echo 1; echo 2; echo 3"

PaulProgrammer
  • 16,175
  • 4
  • 39
  • 56
  • That is what I would expect from `echo $(echo 1; echo 2; echo 3)` but when I read the original I see each expression delimited by `;` which I thought would terminate the first echo. Instead, the first echo *passes over* the `;` which I didn't know was possible... – Display name Oct 13 '18 at 21:33
  • 1
    each token from space to `;` is considered a subcommand (after the first). Subshells are weird. – PaulProgrammer Oct 13 '18 at 21:37
  • Noted. I always thought the shell expansion worked kind of like the -e switch in perl, but I guess there is much more to it than that.. – Display name Oct 13 '18 at 21:40