Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 216
mickp Avatar asked Jun 24 '18 19:06

mickp


People also ask

How does Tab autocomplete work?

Using autocomplete is as simple as pressing the [TAB] and the active command line options will fill-in. If more than one option is available, you can hit [TAB] twice to display all possible choices and continue typing until there is only one matching choice left.

What is tab completion in Linux?

Command-line completion (also tab completion) is a common feature of command-line interpreters, in which the program automatically fills in partially typed commands.

Does bash have tab completion?

Bourne shell and csh do not, but ksh, bash, tcsh, and zsh all have tab completion to varying degrees. The basic principle in all of these shells is the same; you type the start of the word, hit the <TAB> key twice, and the list of possible commands or files is displayed.


1 Answers

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.

like image 100
Arno-Nymous Avatar answered Dec 18 '22 19:12

Arno-Nymous