Search code examples
bashbash-completion

What's the meaning of “the initial non-assignment word on the line” in shell


In Programmable Completion Builtins complete command, there is a -I argument. It said:

The -I option indicates that other supplied options and actions should apply to completion on the initial non-assignment word on the line, or after a command delimiter such as ‘;’ or ‘|’, which is usually command name completion.

I don't understand "the initial non-assignment word on the line" , can someone give an example.

I set a complete by complete -W 'AA BB' -I, It worked "after a command delimiter"

root@aliecs:~# complete -W 'AA BB' -I
root@aliecs:~# ls &&<tab><tab>
AA  BB  
root@aliecs:~# ls ;<tab><tab>
AA  BB  
root@aliecs:~# ls |<tab><tab>
AA  BB  
root@aliecs:~# ls ||<tab><tab>
AA  BB 

I want an example of "the initial non-assignment word on the line"


Solution

  • Shell commands consist of a series of words (where "word" has a formal meaning). An assignment word is one that contains a =:

    foo=5
    

    That's a single word, though in other languages you might consider that the word foo, an =, and a 5, just not separated by whitespace.

    Commands can begin with 0 or more assignment words. An assignment word is not treated as the name of the command to execute, but as a variable assignment that is "local" to that command. For example,

    x=5 foo
    

    is a command that runs a program named foo with a variable x in its environment with the value 5, not a command named x=5 with an argument foo.

    For completion purposes, you don't really want to do completion on assignments, because the whole point of the assignment is to define something that might not already exist, and how would the shell know how to complete something you haven't defined yet? So the complete command ignores the assignment words at the beginning of a command line, and focuses on the first word that could be the name of the command to execute.