Hi Bill, thank you!
Based on your answer, I built the following sub:
sub resolve_envs { my ($path) = @_; while ($path =~ m/[^\\]\$\{?(\w+)\}?/) { my $env = $1; if (exists($ENV{$env})) { $path =~ s/([^\\])\$\{?(\w+)\}?/$1$ENV{$env}/; } else { $path =~ s/([^\\])\$\{?(\w+)\}?/$1\\\$$env/; } } $path =~ s/\\\$/\$/g; return $path; }
It works good for most of the cases. The only corner case that it does not work is that if you have a file that already contains "\$" (not just "$", already escaped). For example:
set playground="${HOME}/playground" set file1=${playground}'/fi$le1' set file2=${playground}'/fi\$le2' mkdir -p ${playground} touch ${file1} touch ${file2}
For the first one it works, but for the second one it does not because I remove the extra backslashes that I added so you get ${playground}'/fi$le2. Actually it won't work for any escaped chars. Any ideas how can I handle this corner case?

In reply to Re^2: How to replace envs in path? by ovedpo15
in thread How to replace envs in path? by ovedpo15

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.