# Bash script auto-complete filename by file type/extension

Suppose I wrote a bash script with usage script.sh filename.type. That is, the script takes in filenames as arguments. Bash’s tab completion works with all files. However, I’ve seen commands that will complete the filename even faster because it will select the file with the “right” extension. For example, pdflatex firstpart[Tab] would automatically select firtpart.whatever.is.next.tex. I’d like this feature for the scripts I write as well. Thanks to this post and this, I found out it is implemented through the complete function in bash. For installed programs like pdflatex, the complete commands are added to /etc/bash_completion through the installer script (I think). For me, I can add them to my ~/.bashrc:

 <pre class="src src-sh"><span style="color: #ff4500;">##</span><span style="color: #ff4500;">complete -G "*.Rnw" pgfSweave</span>


complete -f -X ‘!.@(Rnw)’ pgfSweave Sweave Sweave.sh complete -f -X ‘!.@(eps|ps)’ eps2pdf.sh