fork(1) download
  1. #!/bin/bash
  2. # your code goes here
  3.  
  4. part='\| \$ ; * ( $'
  5.  
  6. string="noquote $part \`hostname\` \"doublequote $part \`hostname\`\" 'singlequote $part \`hostname\`'"
  7.  
  8. #string="\\a jk \"\\a jk\" '\\a jk'" # \ as first char
  9. #string="a\\ jk \"a\\ jk\" 'a\\ jk'" # \ on space
  10. #string="a j\\k \"a j\\k\" 'a j\\k'" # \ mid string
  11. #string="a jk \"a jk\\\" 'a jk\\' \\" # \ as last char
  12.  
  13. pattern='s/[][`~!@#$%^&*():;<>.,?/\|{}=+-]/\\&/g' #;s/\\(.)/\1/g'
  14.  
  15. # within single quotes do nothing, not neccesary, as in, don't apply the pattern at all
  16. # within double quotes remove \ if the following char is in list but not another \
  17.  
  18. echo "$string"
  19. echo "$string" | sed "$pattern"
  20. declare -a "array=($( echo "$string" | sed "$pattern" ))"
  21. for arg in "${array[@]}"; do echo "$arg"; done
  22.  
Success #stdin #stdout 0s 19640KB
stdin
Standard input is empty
stdout
noquote \| \$ ; * ( $ `hostname` "doublequote \| \$ ; * ( $ `hostname`" 'singlequote \| \$ ; * ( $ `hostname`'
noquote \\\| \\\$ \; \* \( \$ \`hostname\` "doublequote \\\| \\\$ \; \* \( \$ \`hostname\`" 'singlequote \\\| \\\$ \; \* \( \$ \`hostname\`'
noquote
\|
\$
;
*
(
$
`hostname`
doublequote \\| \$ \; \* \( $ `hostname`
singlequote \\\| \\\$ \; \* \( \$ \`hostname\`