Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Here goes:

function _command() {
    local cur
    if [ "$COMP_CWORD" -ge "2" ]; then
        COMPREPLY=($( compgen -W '$(pwd)' -- "$cur" ) )
        COMPREPLY=($( compgen -W "arg1 arg2" -- "$cur" ) )
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
up vote 2 down vote accepted


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


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.