当前位置: 动力学知识库 > 问答 > 编程问答 >

escaping - How to pass quoted arguments from variable to bash script

问题描述:

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

#!/bin/bash

for var in "[email protected]"; do

echo "$var"

done

input

[email protected]$ ARGS="-a \"arg one\" -b \"arg two\""

[email protected]$ ./test.sh $ARGS

output

-a

"arg

one"

-b

"arg

two"

expected

-a

arg one

-b

arg two

Note if you pass the quoted arguments directly to the script it works. I also can work around this with eval but I wanted to understand why the first approach failed.

workaround

ARGS="./test.sh -a "arg one" -b "arg two""

eval $ARGS

网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: