How to pass quoted arguments from variable to bash script

前端 未结 1 1578
清歌不尽
清歌不尽 2020-12-10 07:03

I tried building a set of arguments in a variable and passing that to a script but the behavior different from what I expected.

test.sh



        
相关标签:
1条回答
  • 2020-12-10 07:29

    You should use an array, which in some sense provides a 2nd level of quoting:

    ARGS=(-a "arg one" -b "arg two")
    ./test.sh "${ARGS[@]}"
    

    The array expansion produces one word per element of the array, so that the whitespace you quoted when the array was created is not treated as a word separator when constructing the list of arguments that are passed to test.sh.

    Note that arrays are not supported by the POSIX shell, but this is the precise shortcoming in the POSIX shell that arrays were introduced to correct.

    0 讨论(0)
提交回复
热议问题