mikeizbicki / cmc-csci143

big data course materials
40 stars 76 forks source link

Quiz 1 Problem 1 Question #403

Closed henrylong612 closed 9 months ago

henrylong612 commented 9 months ago

Hi @mikeizbicki,

In the process of reviewing the answers for our previous in-class quiz, I have grown increasingly confused about problem 1, which reads as follows:

$ cd; rm -rf quiz; mkdir quiz; cd quiz
$ var="$(echo '$(echo) echo')"
$ touch $var
$ ls | wc -l

The output is 2, and the corresponding files are echo and '$(echo)'. I am confused because the answer does not match what I would expect when I walk through the problem in smaller steps. To start,

$ var="$(echo '$(echo) echo')"

should be equivalent to

$ var="$(echo) echo"

which should be equivalent to

$ var=" echo"

When the final two commands run, I would expect the final output to produce one single file, echo. What am I getting wrong here? The only thing I can think of is that the first step somehow forces the shell to "remember" that the characters in '$(echo) echo' are always string characters and should never be evaluated as code. Thank you in advance for your help, and I'll see you tomorrow!

Confusedly, Henry

mikeizbicki commented 9 months ago

The issue is that the $ substitutions will not get evaluated recursively. So your statement that

$ var="$(echo '$(echo) echo')"

should be equivalent to

$ var="$(echo) echo"

is incorrect. The $ in the output of the first echo will never get evaluated by the shell.

I'd be happy to clarify further tomorrow before/after class.