Zsh-什么是$〜variable扩展,它与$〜== variable不同吗?

时间:2019-09-21 15:16:44

标签: zsh

我从Zsh mailing list找到了这个sudo包装器:

alias sudo='noglob do_sudo '
function do_sudo
{
    integer glob=1
    local -a run
    run=( command sudo )
    if [[ $# -gt 1 && $1 = -u ]]; then
        run+=($1 $2)
        shift ; shift
    fi
    (($# == 0)) && 1=/bin/zsh
    while (($#)); do
        case "$1" in
        command|exec|-) shift; break ;;
        nocorrect) shift ;;
        noglob) glob=0; shift ;;
        *) break ;;
        esac
    done
    if ((glob)); then
        PATH="/sbin:/usr/sbin:/usr/local/sbin:$PATH" $run $~==*
    else
        PATH="/sbin:/usr/sbin:/usr/local/sbin:$PATH" $run $==*
    fi
}

它使用$~==*扩展文件模式。这种扩展称为什么?它记录在哪里? (真的不知道如何搜索,“美元波浪号扩展”,“文件名扩展”,“波浪号扩展”都给了我一些不相关的结果...)

我注意到实际上$~var也可以工作,例如

$ touch foo bar
$ t1='fo*'
$ echo $~t1
foo
$ t2=('fo*' 'ba*')
$ echo $~t2
foo bar

$~==t1有什么不同吗?顺便说一句,在=和变量名之间似乎可以有任意数量的$$~=t1 $=~t1 $~=====t1看起来都一样。

1 个答案:

答案 0 :(得分:1)

感谢@chepner的提示。这是我在import random def generate(low,high,goal_sum,size=15): output = [] for i in range(size): new_int = random.randint(low,high) if sum(output) + new_int <= goal_sum: output.append(new_int) else: output.append(0) random.shuffle(output) return output 手册中找到的内容:

zshexpn

因此,${=spec} Perform word splitting using the rules for SH_WORD_SPLIT during the evaluation of spec, but regardless of whether the parameter appears in double quotes; if the `=' is doubled, turn it off. This forces parameter expansions to be split into separate words before substitution, using IFS as a delimiter. This is done by default in most other shells. Note that splitting is applied to word in the assignment forms of spec before the assignment to name is performed. This affects the result of array assignments with the A flag. ${~spec} Turn on the GLOB_SUBST option for the evaluation of spec; if the `~' is doubled, turn it off. When this option is set, the string resulting from the expansion will be in‐ terpreted as a pattern anywhere that is possible, such as in filename expansion and filename generation and pattern-matching contexts like the right hand side of the `=' and `!=' operators in conditions. In nested substitutions, note that the effect of the ~ applies to the result of the current level of substitution. A surrounding pattern operation on the result may can‐ cel it. Hence, for example, if the parameter foo is set to *, ${~foo//\*/*.c} is substituted by the pattern *.c, which may be expanded by filename generation, but ${${~foo}//\*/*.c} substitutes to the string *.c, which will not be further expanded. 启用了用文件名替换模式(规范)的通配符。 ~确保对引号中的字符串禁用单词拆分。

例如:

==
相关问题