Filename brace expansion in scripts

Colin Law clanlaw at googlemail.com
Thu Nov 18 15:48:36 UTC 2010


I have a problem with filename expansion in a script and am having
difficulty understanding what is going on.
In a terminal, if I enter
ls {a*,b*}
I see all files beginning with a or b (I know this is not the best way
of doing that but it is a simple example of the problem I am seeing).
If I put that line in a file test.sh, make it executable, and then type
./test.sh
then I see the same result.  However if at the start of test.sh I put the line
#!/usr/bin/env sh
at the start of test.sh and then run it I get
ls: cannot access {f*,l*}: No such file or directory

It seems that brace expansion is not being performed in this case.  If
I just use wildcards it is ok, it is the braces that cause the
problem.  In order to determine whether a different shell was running
for some reason I also put
finger -l
in the script, and in both cases, with and without the shebang line,
it showed /bin/bash as the shell.

If, however, I put
#!/bin/bash
at the start then it works, still showing /bin/bash as the shell.

Can someone explain what is going on?

Colin




More information about the ubuntu-users mailing list