How to pass quoted parameters to another program

人走茶凉 提交于 2019-12-02 08:13:55

Use an array; this is why they were introduced.

FOO="ghi"
# Contains one element, but not the literal quotes
DEFAULTS=(--param="abc def $FOO")
# command gets 1 argument, the single element of the array
/some/command "${DEFAULTS[@]}"

# Two elements
DEFAULTS=(--param="abc def $FOO" --other="foo bar")
# command gets 2 arguments
/some/command "${DEFAULTS[@]}"

You are almost there. You just have to put the $DEFAULTS into double quotes so that bash doesn't do the word spliting.

DEFAULTS='param="ac def ghi"'
perl -le'print $ARGV[0]' "$DEFAULTS"

Output:

param="ac def ghi"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!