Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Here goes:

function _command() {
    local cur
    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}
    if [ "$COMP_CWORD" -ge "2" ]; then
        COMPREPLY=($( compgen -W '$(pwd)' -- "$cur" ) )
    else
        COMPREPLY=($( compgen -W "arg1 arg2" -- "$cur" ) )
    fi
}
complete -F _command command

This works well, so long as I source it.

What if I want it to auto-complete both outputs of pwd and some other arbitrary command, say hostname? What if I also wanted it to auto-complete on any file path?

share|improve this question

migrated from stackoverflow.com May 15 '11 at 6:58

This question came from our site for professional and enthusiast programmers.

    
stackoverflow.com/questions/1986023/bash-combine-two-arrays –  Anders Lindahl May 13 '11 at 20:20

1 Answer 1

up vote 2 down vote accepted

Either

COMPREPLY=($(compgen -W '$(pwd)' -- "$cur") $(compgen -W '$(hostname)' -- "$cur"))

(you want a bigger array, just make one) or

COMPREPLY=($( compgen -W '$(pwd; hostname)' -- "$cur" ) )

(still autocompleting one command).

share|improve this answer
    
For the first example, something like $( compgen -o default -- "$cur") does what I wanted... see my question edit. –  Tshepang May 13 '11 at 23:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.