6

While trying to configure the programmable completion, I came across a behavior I don't understand.

Given those three completion scenarios, this is the way I understand them:


$<TAB><TAB>

Beginning of the line --> use complete -E --> if it doesn't exist, use complete -D --> if it doesn't exist, use the default Bash completion --> there are no words on the line --> use command completion.


$com<TAB><TAB>

Trying to complete first word --> use command completion.


$command argu<TAB><TAB>

There is at least one word on the line --> trying to complete an argument --> use complete for the given command --> if it doesn't exist, use complete -D --> if it doesn't exist, use the default Bash completion --> there is already at least one word on the line so don't use command completion --> doesn't start with $, ~ or @ so perform filename completion.


My question is why is the filename completion performed instead of command completion in this scenario even though there are no words on the line yet:

$<SPACE><TAB><TAB>

It's even more confusing since the command completion below is performed as expected:

$<SPACE>com<TAB><TAB>

I most likely misunderstood the manual and I would be most grateful for any explanation.


NOTE: $ denotes the prompt, not a literal dollar sign.

mickp
  • 1,679
  • 7
  • 23
  • 1
    Is the `$` literal (a part of your command that you entered by hand), or are you just using it to represent the command line prompt? For me (using [bash-completion](https://github.com/scop/bash-completion)), a literal `$` shows the list of variables I could complete. `$` assumes `$` is a command without any known parameters and therefore completes with file names by default. – Adam Katz Jul 16 '18 at 04:46
  • @AdamKatz It's there to denote the prompt. I get why that might be misleading and will edit the question accordingly. Thanks. – mickp Jul 16 '18 at 10:27
  • What do you get when you type `complete -p` – kvantour Jul 17 '18 at 07:46

1 Answers1

2

When a line starts with a blank in a Bash shell, this tells the shell that the command should not go into the command history. This can be configured by setting the HISTCONTROL env variable in your .bashrc, .profile or whatever file you want and should be the default behavior (see also How to prevent commands to show up in bash history?).

So command execution is the same when the line has a leading blank except that this command doesn't go into history. Thus, code completion also behaves the same.

Arno-Nymous
  • 495
  • 3
  • 14
  • I am not sure I understand, sorry. So because there is a space, the completion doesn't process commands since space means you don't want them in the command history etc? And how come `$com` works even though there is also a space? – mickp Jun 24 '18 at 19:55
  • 2
    I believe I've slighty mistaken your question. Maybe this helps you a bit more: https://unix.stackexchange.com/questions/205360/how-does-bash-auto-completion-work#205409 According to (2) you are outside of the context for command completion (since you haven't started a word yet but already typed a character), thus Bash tries to auto-complete filenames. Hope this gives you some guidance :) – Arno-Nymous Jun 24 '18 at 20:45