简体   繁体   中英

Bash joining array

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\""

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" "$@"' "$@"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM