#!/bin/bash
# your code goes here
part='\| \$ ; * ( $'
string="noquote $part \`hostname\` \"doublequote $part \`hostname\`\" 'singlequote $part \`hostname\`'"
#string="\\a jk \"\\a jk\" '\\a jk'" # \ as first char
#string="a\\ jk \"a\\ jk\" 'a\\ jk'" # \ on space
#string="a j\\k \"a j\\k\" 'a j\\k'" # \ mid string
#string="a jk \"a jk\\\" 'a jk\\' \\" # \ as last char
pattern='s/[][`~!@#$%^&*():;<>.,?/\|{}=+-]/\\&/g' #;s/\\(.)/\1/g'
# within single quotes do nothing, not neccesary, as in, don't apply the pattern at all
# within double quotes remove \ if the following char is in list but not another \
echo "$string"
echo "$string" | sed "$pattern"
declare -a "array=($( echo "$string" | sed "$pattern" ))"
for arg in "${array[@]}"; do echo "$arg"; done
IyEvYmluL2Jhc2gKIyB5b3VyIGNvZGUgZ29lcyBoZXJlCgpwYXJ0PSdcfCBcJCA7ICogKCAkJwoKc3RyaW5nPSJub3F1b3RlICRwYXJ0IFxgaG9zdG5hbWVcYCBcImRvdWJsZXF1b3RlICRwYXJ0IFxgaG9zdG5hbWVcYFwiICdzaW5nbGVxdW90ZSAkcGFydCBcYGhvc3RuYW1lXGAnIgoKI3N0cmluZz0iXFxhIGprIFwiXFxhIGprXCIgJ1xcYSBqayciCQkjIFwgYXMgZmlyc3QgY2hhcgojc3RyaW5nPSJhXFwgamsgXCJhXFwgamtcIiAnYVxcIGprJyIJCSMgXCBvbiBzcGFjZQojc3RyaW5nPSJhIGpcXGsgXCJhIGpcXGtcIiAnYSBqXFxrJyIJCSMgXCBtaWQgc3RyaW5nIAojc3RyaW5nPSJhIGprIFwiYSBqa1xcXCIgJ2EgamtcXCcgXFwiCQkjIFwgYXMgbGFzdCBjaGFyCgpwYXR0ZXJuPSdzL1tdW2B+IUAjJCVeJiooKTo7PD4uLD8vXHx7fT0rLV0vXFwmL2cnICM7cy9cXCguKS9cMS9nJwoKIyB3aXRoaW4gc2luZ2xlIHF1b3RlcyBkbyBub3RoaW5nLCBub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