0

Is there a way to join (with proper quoting and escaping) a bash array such that it gives me a string suitable for passing to bash -c?

Reason: I want to write a function foo, such that when I do this:

foo app "hello world.txt"

it'll do this:

sudo -u myuser ENVVAR=somevalue bash -c "perl app \"hello world.txt\""

1 Answer 1

1

bash -c takes the first argument as the command string, and assigns the remaining arguments to positional arguments starting from $0. So, you don't need to quote anything, just pass the arguments:

function foo () {
    sudo -u myuser ENVVAR=somevalue bash -c 'perl "$0" "$@"' "$@"
}
Sign up to request clarification or add additional context in comments.

Comments