使用运算符j处理zsh数组

mpi*_*tas 4 shell zsh

下面的代码取自这里:

function +vi-git-st() {
    local ahead behind remote
    local -a gitstatus

    # Are we on a remote-tracking branch?
    remote=${$(git rev-parse --verify ${hook_com[branch]}@{upstream} \
        --symbolic-full-name 2>/dev/null)/refs\/remotes\/}

    if [[ -n ${remote} ]] ; then
        # for git prior to 1.7
        # ahead=$(git rev-list origin/${hook_com[branch]}..HEAD | wc -l)
        ahead=$(git rev-list ${hook_com[branch]}@{upstream}..HEAD 2>/dev/null | wc -l)
        (( $ahead )) && gitstatus+=( "${c3}+${ahead}${c2}" )

        # for git prior to 1.7
        # behind=$(git rev-list HEAD..origin/${hook_com[branch]} | wc -l)
        behind=$(git rev-list HEAD..${hook_com[branch]}@{upstream} 2>/dev/null | wc -l)
        (( $behind )) && gitstatus+=( "${c4}-${behind}${c2}" )

        hook_com[branch]="${hook_com[branch]} [${remote} ${(j:/:)gitstatus}]"
    fi
}
Run Code Online (Sandbox Code Playgroud)

我不明白最后一行.变量gitstatus是一个数组,那${(j:/:)gitstatus}应该怎么办?我知道它输出字符串,first_array_element/second_array_element但我没有找到任何关于运算符的文档j.这是一些特定的zsh功能,还是标准的shell编程结构?

Sha*_*hin 17

这是连接数组元素的参数扩展标志.见(j:...... :)标志.

在该特定情况下,它使用/作为分隔符来连接阵列内的元素.例如

zsh% foo=(1 2 3)    
zsh% echo $foo
1 2 3
zsh% echo ${(j:/:)foo}
1/2/3
Run Code Online (Sandbox Code Playgroud)

  • 谢谢!很难在谷歌上搜索此类信息。 (2认同)
  • 要明确回答一个未解决的问题:是的,这是zsh的特定功能。(用zsh提示提示) (2认同)
  • 如果我想加入`:`怎么办? (2认同)
  • zshexpn手册页:"任何字符,或匹配的对'(...)','{...}','[...]'或'<...>',可以使用到位冒号作为分隔符,但请注意,当一个标志占用多个参数时,一对匹配的分隔符必须包围每个参数." (2认同)