Why does "$<space><tab><tab>" perform filename completion?
Asked Answered
A

1

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.

Almeta answered 24/6, 2018 at 19:28 Comment(3)
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), a literal $<tab><tab> shows the list of variables I could complete. $<space><tab><tab> assumes $ is a command without any known parameters and therefore completes with file names by default.Greatniece
@AdamKatz It's there to denote the prompt. I get why that might be misleading and will edit the question accordingly. Thanks.Almeta
What do you get when you type complete -pAesculapius
D
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.

Dermis answered 24/6, 2018 at 19:35 Comment(2)
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 $<SPACE>com<TAB><TAB> works even though there is also a space?Almeta
I believe I've slighty mistaken your question. Maybe this helps you a bit more: unix.stackexchange.com/questions/205360/… 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 :)Dermis

© 2022 - 2024 — McMap. All rights reserved.