Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you need these...</p> <h2>Features</h2> <ul> <li>Single line and inline comments; </li> <li>Trimming spaces around <code>=</code> (ie <code>var = value</code> will not fail);</li> <li>Quoted string values;</li> <li>Understanding of DOS line endings;</li> <li>Keep safe, avoiding sourcing your config file.</li> </ul> <h2>Code</h2> <pre><code>shopt -s extglob configfile="dos_or_unix" # set the actual path name of your (DOS or Unix) config file tr -d '\r' &lt; $configfile &gt; $configfile.unix while IFS='= ' read -r lhs rhs do if [[ ! $lhs =~ ^\ *# &amp;&amp; -n $lhs ]]; then rhs="${rhs%%\#*}" # Del in line right comments rhs="${rhs%%*( )}" # Del trailing spaces rhs="${rhs%\"*}" # Del opening string quotes rhs="${rhs#\"*}" # Del closing string quotes declare $lhs="$rhs" fi done &lt; $configfile.unix </code></pre> <h2>Comments</h2> <p><code>tr -d '\r' ...</code> deletes DOS carriage return.<br> <code>! $lhs =~ ^\ *#</code> skips single line comments and <code>-n $lhs</code> skips empty lines.<br> Deleting trailing spaces with <code>${rhs%%*( )}</code> requires setting extended globbing with <code>shopt -s extglob</code>. (Apart using <code>sed</code>), you can avoid this, via the more complex: </p> <pre><code>rhs="${rhs%"${rhs##*[^ ]}"}" </code></pre> <h2>Test config file</h2> <pre class="lang-none prettyprint-override"><code>## This is a comment var1=value1 # Right side comment var2 = value2 # Assignment with spaces ## You can use blank lines var3= Unquoted String # Outer spaces trimmed var4= "My name is " # Quote to avoid trimming var5= "\"Bob\"" </code></pre> <h2>Test code</h2> <pre><code>echo "Content of var1 is $var1" echo "Content of var2 is $var2" echo "Content of var3 is [$var3]" echo "Content of var4 + var5 is: [$var4$var5]" </code></pre> <h2>Results</h2> <pre class="lang-none prettyprint-override"><code>Content of var1 is value1 Content of var2 is value2 Content of var3 is [Unquoted String] Content of var4 + var5 is: [My name is "Bob"] </code></pre>
    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. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    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