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?

  • Why don't you use DATA="$(cat file.hex | xxd -r | tr -d "$(cat trim.txt)")"? – cuonglm Jun 8 '15 at 18:22

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.


You don't need to escape the nested quotes inside. They get parsed properly, surprisingly!

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

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.