Disable sorting of COMPREPLY in bash complete function

I have a list of words that should be present in the order of appearance in completion to a certain key, but bash seems to be internally sorting what is assigned to COMPREPLY array. How to avoid that?


_comm() {
    _init_completion -s -n : || return
    case $prev in
        -a) COMPREPLY=(zxy abcdef tyuu fgsfds)
complete -F _comm comm

If you run this code, bash should complete

$ comm -a 


abcdef fgsfds tyuu zxy

i.e. sorted alphabetically.

Asked By: tijagi


This should be achieved by modifying the complete command:

complete -F _comm comm


complete -F _comm comm -o nosort 

As mentioned in the documentation:

however I didn’t get this to work on bash 4 :/

It seems it is a documented, yet not implemented feature of controlling readline’s variable rl_sort_completion_matches.

More info

Answered By: coderofsalvation

Since Bash 4.4 you can use nosort option. In your example change the last line to:

complete -o nosort -F _comm comm

and you should get completions without alphabetical sorting.

Important note: options (specified with -o) must precede functions (-F). That’s why coderofsalvation’s code didn’t work.

Answered By: O'Fermah
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.