fork download
  1. # Built as an answer to the question in https://stackoverflow.com/questions/47563387/bash-unescape-string/47563868?noredirect=1#comment107466563_47563868
  2. buildInput() {
  3. printf '%s\n' \
  4. 'Quoted phrase: "two words"' \
  5. 'Two tabs: <\t\t>' \
  6. 'Two backslashes: \\\\'
  7. }
  8. hdr() { printf -- '------\n-- %s\n' "$@"; }
  9.  
  10. hdr 'Original stream:'
  11. buildInput
  12.  
  13. hdr 'Desired output:'
  14. printf '%b\n' "$(buildInput)"
  15.  
  16. hdr 'Original proposal:'
  17. buildInput | xargs -I {} printf '%b\n' {}
  18.  
  19. hdr 'Without -I {}:'
  20. buildInput | xargs printf '%b\n'
  21.  
  22. hdr 'Correct output only after using -d:'
  23. buildInput | xargs -d $'\n' printf '%b\n'
  24.  
  25. hdr '...or using -0, transforming the input to be NUL-delimited:'
  26. buildInput | tr '\n' '\0' | xargs -0 printf '%b\n'
Success #stdin #stdout 0s 4352KB
stdin
Standard input is empty
stdout
------
-- Original stream:
Quoted phrase: "two words"
Two tabs: <\t\t>
Two backslashes: \\\\
------
-- Desired output:
Quoted phrase: "two words"
Two tabs: <		>
Two backslashes: \\
------
-- Original proposal:
Quoted phrase: two words
Two tabs: <tt>
Two backslashes: \
------
-- Without -I {}:
Quoted
phrase:
two words
Two
tabs:
<tt>
Two
backslashes:
\
------
-- Correct output only after using -d:
Quoted phrase: "two words"
Two tabs: <		>
Two backslashes: \\
------
-- ...or using -0, transforming the input to be NUL-delimited:
Quoted phrase: "two words"
Two tabs: <		>
Two backslashes: \\