mercredi 25 mars 2015

Command substitutions vs backslash escapes in a quoted string


Consider a double-quoted command substitution with backslash escapes inside it, like this:



echo "$(echo '\\')"


It prints out \\, whereas I would have expected it to print out only one backslash. My thinking (which is incorrect) was that it went through the whole string, replacing all backslash escapes, and then it would do the command substitution. Evidently, the order is opposite.


However, if we do



echo "$(echo '\')$"


it prints out \$. I would've expected that if it's doing the command substitutions first and then evaluating that string's backslash escapes, that the \ and the $ might combine to make a single $ in the end. But it doesn't.


Where do backslash escapes fit into the order of things?


(The context for this question is that I'm working on something that will properly escape regex characters in a string for insertion into a sed command.)



Aucun commentaire:

Enregistrer un commentaire