Hacker News new | ask | show | jobs
by databasher 2465 days ago
Why not use bash's printf %q?

   string=$(printf ' %q' "${array[@]}")
1 comments

In Bash ≥ 4.4, you can also use parameter transformations:

    function quote() {
      echo "${@@Q}"
    }
(In both cases, the generated output is understood bash, but not necessarily by other POSIX-compliant shells.)