miércoles, 19 de diciembre de 2018

A strange case of quoting shell vars

cat << ${FOO:-"{}"}
EOF


You got that? evaluate it in your shell. Now Put it into a file and try to "sh file.sh".  You're going to see the funny {"}.   Go figure...

  1. sh doesn't understand about nested {} inside {}
  2. Take into account that bash or zsh do, so none of this applies there, you have to test it on pure sh.
  3. ${FOO:-"{} is what gets interpreted as part of the variable
  4. as $FOO is not defined, '"{' should be outputted,
  5. leaving the final "{"}
  6. But nope. Because quotes inside a variable expansion do not pass through
  7. So the final {"}
  8. Bonus points for this not happening in bash
  9. More bonus points for this being inside a makefile that calls the file as $(SHELL) file.sh. And different platforms use different $(SHELL) by default.
  10. Mine (ubuntu) uses /bin/sh (which is linked to /bin/dash), others might use busybox, and MacOS uses something that does not do any of the above (zsh?)

No hay comentarios: