Bash 如何避免扩展“eval集合”中的单引号文本&引用$@";`对于getopt?

Bash 如何避免扩展“eval集合”中的单引号文本&引用$@";`对于getopt?,bash,shell,wildcard,getopt,quoting,Bash,Shell,Wildcard,Getopt,Quoting,我使用传统模式通过getopt执行参数解码: function mytest { eval set -- `getopt --options h --long help -- "$@"` echo "1:$1 2:$2" } 但当我传递一个带引号的字符串时,它实际上是展开的,例如: $ mytest 'x * z' 1:-- 2:<list of files from current dir> 如何按预期执行评估?因为您在eval中使用,所以我看不到除禁用globs之外的

我使用传统模式通过getopt执行参数解码:

function mytest {
  eval set -- `getopt --options h --long help -- "$@"`
  echo "1:$1 2:$2"
}
但当我传递一个带引号的字符串时,它实际上是展开的,例如:

$ mytest 'x * z'
1:-- 2:<list of files from current dir>

如何按预期执行评估?

因为您在
eval
中使用,所以我看不到除禁用globs之外的其他选项(扩展发生在命令实际运行之前),即:


引用您的扩展以防止全球化:

function mytest {
   eval set -- "`getopt --options h --long help -- "$@"`"
   echo "1:$1 2:$2"
}

请注意,
set-h
set-o hashall
)与
set--help
不同,这似乎不是读取可选参数Shello的传统方式!对不起,我不明白这个评论
-h
/
--help
不是
getopt
命令的选项,而是函数的选项。换句话说,例如,使用
eval set--`getopt--options u--long-uselessoption--“$@”`
仍然会得到相同的结果。这也是由感谢自动建议的!您能否详细说明一下,没有您的建议,
mytest'*z'
没有展开,而
mytest'x*z'
是展开的原因?@Marcus
foo*
匹配所有以
foo
开头的文件<代码>'*匹配以单引号开头的所有文件。你可能没有。
mytest ()
{
    set -f    # disable file name generation (globbing).
    eval set -- $(getopt --options h --long help -- "${@}")
    echo "1:${1} 2:${2}"
    set +f
}

$ mytest x * z
1:-- 2:x

$ mytest 'x * z'
1:-- 2:x * z

$ mytest ./*
1:-- 2:./config-err-reCeGT

$ mytest "./*"
1:-- 2:./*
function mytest {
   eval set -- "`getopt --options h --long help -- "$@"`"
   echo "1:$1 2:$2"
}