1

I am attempting to write a bash parser. Many resources have referred to this wiki

One area I am getting stuck is why the following would work

echo "$(echo "hi")"    # output => hi

According to the wiki, quotes are parsed before command substitution. In that case, why wouldn't there be an error parsing the first set of double quotes i.e."$(echo "

1
  • 1
    "I am attempting to write a bash parser" That's the endeavour of a lifetime. Only bash can parse the bash language. Really. Besides the complexity and inconsistence of the shell language, its dynamic nature and the fact that your supposed to parse and run a script line by line means that you cannot parse a bash script without actually running it.
    – user414777
    Sep 17, 2020 at 13:29

1 Answer 1

7

The linked article says:

all characters except for $, ", ` and \ lose any special meaning they might have

[emphasis mine]

What happens is $ in $( in your code is still special. $( ) is the syntax of command substitution. About command substitution, from the same site:

Notably, once inside a command substitution, the shell begins an entirely new quoting context. That is, double quotes inside the substitution do not match up with double quotes outside the substitution.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.