Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use this:</p> <pre><code>perl -p -i -e 's/\$\{([^}]+)\}/defined $ENV{$1} ? $ENV{$1} : $&amp;/eg' &lt; template.txt </code></pre> <p>to replace all <code>${...}</code> strings with corresponding enviroment variables (do not forget to export them before running this script).</p> <p>For pure bash this should work (assuming that variables do not contain ${...} strings):</p> <pre><code>#!/bin/bash while read -r line ; do while [[ "$line" =~ (\$\{[a-zA-Z_][a-zA-Z_0-9]*\}) ]] ; do LHS=${BASH_REMATCH[1]} RHS="$(eval echo "\"$LHS\"")" line=${line//$LHS/$RHS} done echo "$line" done </code></pre> <p>. Solution that does not hang if RHS references some variable that references itself:</p> <pre> #!/bin/bash line="$(cat; echo -n a)" end_offset=${#line} while [[ "${line:0:$end_offset}" =~ (.*)(\$\{([a-zA-Z_][a-zA-Z_0-9]*)\})(.*) ]] ; do PRE="${BASH_REMATCH[1]}" POST="${BASH_REMATCH[4]}${line:$end_offset:${#line}}" VARNAME="${BASH_REMATCH[3]}" eval 'VARVAL="$'$VARNAME'"' line="$PRE$VARVAL$POST" end_offset=${#PRE} done echo -n "${line:0:-1}" </pre> <p><strong>WARNING</strong>: I do not know a way to correctly handle input with NULs in bash or preserve the amount of trailing newlines. Last variant is presented as it is because shells “love” binary input:</p> <ol> <li><code>read</code> will interpret backslashes.</li> <li><code>read -r</code> will not interpret backslashes, but still will drop the last line if it does not end with a newline.</li> <li><code>"$(…)"</code> will strip as many trailing newlines as there are present, so I end <code>…</code> with <code>; echo -n a</code> and use <code>echo -n "${line:0:-1}"</code>: this drops the last character (which is <code>a</code>) and preserves as many trailing newlines as there was in the input (including no).</li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload