Filename brace expansion in scripts
Colin Law
clanlaw at googlemail.com
Thu Nov 18 16:13:26 UTC 2010
On 18 November 2010 16:01, Loïc Grenié <loic.grenie at gmail.com> wrote:
> 2010/11/18 Colin Law <clanlaw at googlemail.com>:
>> 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.
>
> This is your login shell, not the shell running the script.
OK, looking at man finger again I see you are right. I was mislead by
google, which is not unusual of course.
>
>> 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?
>
> If you do not put the shebang at the beginning of the script, then bash
> executes it. If you put the shebang it is executed by what you say, i.e.
> /usr/bin/env that calls sh. Try #!/usr/bin/env bash
I see, I don't now know where I got into the habit of putting
usr/bin/env sh there. Somehow I had understood that as the
conventional thing to do. ls -al /bin/sh shows that this is a link to
dash. Why does dash not do brace expansion I wonder.
>
> Hope this helps,
It certainly does, many thanks
Colin
More information about the ubuntu-users
mailing list