Search code examples
bashescaping

Convert bash array to a single stringified shell argument list, handable by "sh -c"


This question looks similar to many questions on SO, but I didn't find a real answer.

Sometimes I need to run sh -c "$my_command", where $my_command has to be created from a bash array of arguments (whatever the arguments look like).

Here is my solution, but I wonder if there is a better, (maybe native) bash solution for this?

#!/bin/bash

# My solution:
args_to_shell_escaped_string() {
    local arg
    for arg in "$@"; do
        printf '%q ' "$arg"
    done
}

# Testing it:
args=(
    'arg with spaces'
    'arg with; !! any { special $char'
)

escaped_args=$(args_to_shell_escaped_string "${args[@]}")

sh -c "
for a in $escaped_args; do
    echo \">> \$a\"
done
"

Solution

  • Yes, there is the Q parameter transformation.

    sh -c "
    for a in ${args[*]@Q}; do
        echo \">> \$a\"
    done
    "