Filename brace expansion in scripts
Loïc Grenié
loic.grenie at gmail.com
Thu Nov 18 16:01:15 UTC 2010
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.
> 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
Hope this helps,
Loïc
More information about the ubuntu-users
mailing list