Filename brace expansion in scripts

Kenneth Loafman kenneth at loafman.com
Thu Nov 18 18:34:01 UTC 2010


On Thu, Nov 18, 2010 at 9:48 AM, Colin Law <clanlaw at googlemail.com> wrote:

> 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?
>

Yes, your default shell is probably /bin/bash, which is why your console
entry works.  By specifying the use of 'sh' you get a shell from /bin/dash
(note bash vs dash).  If you specify "#!/usr/bin/env bash" instead, your
script will work much like your console.

...Ken
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20101118/700aa7cb/attachment.html>


More information about the ubuntu-users mailing list