[Novalug] Fill in the blanks?
James Ewing Cottrell 3rd
JECottrell3 at Comcast.NET
Fri Mar 16 19:28:06 EDT 2012
I appreciate Karri's attempt to spare my feelings an ego, this really
needs to be corrected.
Another way of looking at this is that # is a 'minimal' pattern and ##
is a maximal one. In the minimal case, the * matches nothing, while in
the maximal case, it matches 'c'.
So "echo ${file#/*/} will give b/c, and if we want the leading slash
back, we can add it before the $, as in
"echo /${file#/*/}" will give /b/c and "echo /${file##/*/}" will give /c
Which brings me to another point. Often when I right shell scripts I
will echo the commands rather than executing them.
This lets me debug them by inspection. The last thing I want to do is
rename a list of files to a constant name, losing all but the last one.
When I am done, I will pipe to sh or sh -x if I am feeling voyeuristic.
Or sometimes I will grab a subset with the mouse and cut and paste.
JIM
P.S. Bash Me Harder
On 3/16/2012 5:25 PM, Karri Balk wrote:
> I'm not sending this to the group:
>
>> ${x#string} removes string from the left of x, ${x%string} removes it
>> from the right. String uses filename matching, where ? is one character,
>> and * is many. Presumably, [y-z] character classes work too, but I never
>> use them. Doubling the # or % makes the matching greedy, so if file is
>> /a/b/c, the ${file#/*} gives /b/c and ${file##/*} gives /c and
>> ${file##*/} gives c.
> ${file#/*} will give you a/b/c
> ${file##/*} will give you nothing
>
> Karri
>
More information about the Novalug
mailing list