Bash – Nested quotes in subshells

bashcommand-substitutionquoting

Say I have to use quotes to encapsulate subshell output like:

DATA="$(cat file.hex | xxd -r)"

But I need to nest this kind of stuff like:

DATA="$(cat file.hex | xxd -r | tr -d \"$(cat trim.txt)\")"

I can't use single quotes because those do not expand variables that are inside of them. Escaping quotes doesn't work because they are just treated as passive text.

How do I handle this?

Best Answer

You don't need to escape the quotes inside a subshell, since the current shell doesn't interpret them (it doesn't interpret anything from $( to ), actually), and the subshell doesn't know about any quotes that are above.

Quoting a subshell at variable assignment is unnecessary too, for more info see man bash.

Related Question