Darn you, failglob

I turned on bash_completition today. Couldn't quite remember why I had it turned off. I quickly realized though. bash would fail on any filename with a square-bracket in it. It looks something like this:

~ $ touch 'test [1]'
~ $ ls<I HIT TAB HERE> bash: no match: test [1]
~ $

In other words, hitting TAB to tab-complete filenames would dump me back to a command prompt giving me that unhelpful error message, and I'd have to start the whole command over.

The problem was that I had this in my ~/.bashrc:

shopt -s failglob

I can't for the life of me remember why that was in my .bashrc. Clearly it wasn't doing anything I never had any use for, because this is the first time I got this "bash: no match" error. Removing that line makes bash_completition work again though, so all is well.

This was a nightmare to debug too. Google was totally useless. I tracked it down to the _filedir function in /etc/bash_completition, and then it was trial-and-error. Next time I'm going to start off loading a clean login environment before I bother checking the system scripts for things. I did also learn that set -v in a bash script makes it dump every line it executes to STDIN as it's executing them.

Tags: ,

2 Responses to “Darn you, failglob”

  1. Quoth Richard Neill:

    I can tell you why failglob is good and why it is bad.
    Consider a shell script, running:

    i=0; for file in *ZZZ; do let i++; done; echo “There are $i files matching ‘*.ZZZ'”

    [This should count the number of files ending in the .ZZZ extension]

    What happens if you haven't got any files ending in .ZZZ?

    By default, bash will fail to expand (glob) “*.ZZZ”, and will fall-back to evaluating it as a literal ‘*.ZZZ'.

    You will get the wrong answer, i.e. 1.

    failglob will make bash throw an error. nullglob is probably what you want.

  2. Quoth Brian:

    Yeah, that makes sense. Thanks for clearing that up.

    When I set nullglob, if I do `ls *ZZZ` it actually does `ls` if the glob matches nothing. That seems kind of dangerous for any command that defaults to doing something on all files if the file list is missing. Globs are so messy. Can't live with them, can't live without them.

Leave a Reply

You can use these tags in comments (Note: HTML is automatically escaped inside <pre> tags, nowhere else, so if you post source code, put it in <pre>):

<pre lang="some_programming_language"> 
<em>
<strong>
<a href="url">

NOTE: Comments are automatically spam-filtered. If your comment fails to appear, it was likely munched by the filter. Try not to link-spam or post anything that looks like it was typed by a robot.