简体   繁体   中英

Bash Tab Completion of Filenames after Arguments

I have recently moved from Scientific Linux 6 to CentOS 7 and am having an issue with bash tab completion in the new OS.

Software Versions

$ cat /etc/redhat-release
CentOS Linux release 7.1.1503 (Core)

$ uname -r
3.10.0-229.14.1.el7.x86_64

$ bash --version
GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)

I have a bash script (executable) named ./run_prog.sh that takes a configuration file on the command line using the -c option (or long --config=).

Example of full command:

./run_prog.sh -c=./config/test-new-feature.conf
## or
./run_prog.sh --config=./config/test-new-feature.conf

In previous versions of bash i was able to tab complete directory and filenames after the -c= construct.

Example of expected tab complete (how it worked in SL6):

./run_prog.sh -c=./conf[TAB]
 ## completes to
./run_prog.sh -c=./config/
 ## then type
./run_prog.sh -c=./config/test-n[TAB]
 ## completes to
./run_prog.sh -c=./config/test-new-feature.conf

The new version of bash in CentOS 7 will not complete any filenames after the -c= short option.

Example of broken tab completion in CentOS 7:

./run_prog.sh -c=./conf[TAB]
 ## doesn't complete anything
./run_prog.sh -c=./conf

However if i separate the -c with a space the filename completion works as expected.

Example of working tab completion with space:

./run_prog.sh -c ./conf[TAB]
 ## completes to
./run_prog.sh -c ./config/
 ## then type
./run_prog.sh -c ./config/test-n[TAB]
 ## completes to
./run_prog.sh -c ./config/test-new-feature.conf

Question

How can i get the new version of bash to tab complete filenames like the old version of bash did?

Edit

This script has an long version for the short -c option that is --config. The long version doesn't work either.

./run_prog.sh --config=./conf[TAB]
 ## doesn't complete anything
./run_prog.sh --config=./conf

This makes me thing that bash is getting confused by the lack of spacing between the option switch (-c) and the option value.

My old bash-completion directory

$ ls -1 /media/old_hd/etc/bash_completion.d/
bzr
createrepo.bash
dkms
fcoeadm
fcoemon
gdbus-bash-completion.sh
git
gvfs-bash-completion.sh
lldpad
lldptool
perf
phoronix-test-suite
pk-completion.bash
rpmdevtools.bash-completion
subversion
yum.bash
yum-utils.bash

My new bash_completion directory

$ ls -1 /etc/bash_completion.d/
createrepo
dkms
fcoeadm
fcoemon
genpkgmetadata.py
git
lldpad
lldptool
mergerepo
mergerepo.py
modifyrepo
modifyrepo.py
redefine_filedir
scl.bash
yum
yummain.py
yum-utils.bash

After googling (and help from friends) i've found the solution!

To fix filename auto completion add the following to your ~/.bashrc file:

complete -D -o default

Reference: Bash completion for path in argument (with equals sign present)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM