使用运算符 j 操作 zsh 数组 [英] Manipulate zsh arrays with operator j

查看:20
本文介绍了使用运算符 j 操作 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
}

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

I do not understand the last line. Variable gitstatus is an array, so what is ${(j:/:)gitstatus} supposed to do? I know that it outputs the string first_array_element/second_array_element but I did not manage to find any documentation about operator j. Is this some specific zsh feature, or is it standard shell programming construct?

推荐答案

那是连接数组元素的参数扩展标志.请参阅(j:...:) 标志.

That's the parameter expansion flag which joins array elements. See (j:...:) Flag.

在这种特定情况下,它使用 / 作为分隔符连接数组中的元素.例如

In that specific case, it joins the elements within the array using / as the separator. E.g.

zsh% foo=(1 2 3)    
zsh% echo $foo
1 2 3
zsh% echo ${(j:/:)foo}
1/2/3

这篇关于使用运算符 j 操作 zsh 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆