Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manipulate zsh arrays with operator j

Tags:

shell

zsh

The following code is taken from here:

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
}

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?

like image 882
mpiktas Avatar asked May 29 '12 09:05

mpiktas


1 Answers

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
like image 161
Shawn Chin Avatar answered Jan 03 '23 14:01

Shawn Chin