export PATH=`perl -e 'print join ":", (grep $h{$_}++ < 1, split /:/,$E
+NV{PATH}), "\n" ;'`
Nuts and Bolts
Unix path specifications are delimited by colons(:). The line breaks the existing entry into a list of separate directories
1. It then iterates over that list, at each directory it references an entry in the %h hash and increments it
2. If the entry previously did not exist, the entry will be undef, the entry will be added to a new list. If the entry already exists, it will be skipped. The resulting list is then passed to 'join' where the entries will be joined into a string, with a ":" between each entry
3. The result is printed
4. This expression is evaluated by the -e option of perl
5 and put back into the PATH variable with the ``
6.
1 split/:/, $ENV{PATH} produces a list of separate entries. PATH=/usr/bin:/usr/ccs/bin:/bin, becomes (/usr/bin, /usr/ccs/bin, /bin)
2 grep $h{$_}++ < 1 grep iterates over a list, in this case, the list produced by the split in 1. At each step it sets $_ to the current entry and evaluates the expression "$h{$_}++ < 1". $h{$_}++ does a lookup in the %h hash and increments it and compares tests the pre-increment value to see if it's less than one. When the hash does not already contain an entry for $_ (the current directory being examined) this test will be TRUE, and simulaneously puts a 1 into that slot in the hash. If the expression sees another entry for the same directory that test will now be FALSE, and so the entry will not be included in grep's result.
3 join ":" takes the result of the grep and catenates the entries together, separating them with ":".
4 print outputs the whole result, a new string that will be suitable for a PATH variable.
5 perl -e '...' The -e str command line option tells perl to take the string between the '' and evaluate it.
6 export PATH=`cmd` (or setenv PATH `cmd` for csh) executes the command between the backticks(` very different from the ' or ") and sets PATH to the value of the result.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.