[oclug] set $PATH

Ross Jordan rjordan at student.math.uwaterloo.ca
Thu Jul 10 14:56:15 EDT 2003


> 
> On 10/07/03 Bruce Harding did speaketh:
> 
> > When setting $PATH can I use a wild card?
> >=20
> > I.E.  export PATH=3D$PATH:/opt/OpenOffice.org*/program
> 
>     Hmm. You've actually come across some odd shell behaviour.=20
> 
> [moulier at tigger msoulier]$ foo=3D$PATH
> [msoulier at tigger msoulier]$ export foo=3D$foo:/usr/local/moz*
> [msoulier at tigger msoulier]$ echo $foo
> /usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/msoulier/bin:/sbin:/usr/s=
> bin:/usr/local/moz*
> [msoulier at tigger msoulier]$ which mozilla
> [msoulier at tigger msoulier]$ type -all mozilla
> bash: type: mozilla: not found
> 
>     So it's actually putting * into variable itself.=20
> 
>     If I do this such that the variable contents are a single path,
> wildcard expansion works.=20
> 
> [msoulier at tigger msoulier]$ MOZ=3D/usr/local/moz*
> [msoulier at tigger msoulier]$ echo $MOZ
> /usr/local/mozilla

The shell expansion occurs during the echo command:

sparta root # MOZ=/usr/lib/moz*
sparta root # echo $MOZ
/usr/lib/mozilla
sparta root # echo "$MOZ"
/usr/lib/moz*

>     But, the * is still in the variable. Hidden away.
Indeed; the variable is /usr/lib/moz* (or /usr/local/moz*).
The expansion of the * comes later.

> [msoulier at tigger msoulier]$ foo=3D$PATH
> [msoulier at tigger msoulier]$ export foo=3D$foo:$MOZ
> [msoulier at tigger msoulier]$ echo $foo
> /usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/msoulier/bin:/sbin:/usr/s=
> bin:/usr/local/moz*
> 
>     That seems a little broken to me. I didn't know bash would do that.=20
> 
>     To answer your question, it would appear not. You can probably
> achieve the same result if you're more clever, and work a little harder.
> Frankly, I think it should work, and I'm tempted to call this a bug.

I wouldn't call it broken, it mostly makes sense.

One way to make it work:
for file in `/usr/lib/moz*`; do PATH=$PATH:$file; done

Although, one probably only wants dirs:
for dir in `find /usr/lib/moz* -type d`; do PATH=$PATH:$dir; done

-Ross




More information about the OCLUG mailing list