Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Little introduction</h2> <p>Tracking forks (like <code>$()</code>) as they consume lot more ressource than normal (sequential) execution, if you know which variable has to be modified in advance, you'll better to use <code>local</code> definition around your whole script and use function instead of having to make file access between commands:</p> <pre><code>#!/bin/bash # There is the `A` script local VAR function B() { read -a VAR &lt;/proc/uptime } B echo $VAR sleep 1 B echo $VAR </code></pre> <p>could output something like:</p> <pre><code>1610291.39 1610292.39 </code></pre> <p>with time to access <code>/bin/sleep</code> could be less than 1/100th of second.</p> <h3>A little demonstration:</h3> <pre><code>local V1 B1() { read -a V1 &lt;/proc/uptime } B1 ;echo $V1 ;for i in {0..10000} ;do B1 ;done ;echo $V1 1610843.78 1610844.38 printf "%d 1/100th of sec\n" $((161084438-161084378)) 60 1/100th of sec </code></pre> <p>I could set 10000x my variable in 60/100 seconds, while</p> <pre><code>B2() { local var read -a var &lt;/proc/uptime echo $var } V2=$(B2);echo $V2;for i in {0..10000};do V2=$(B2);done;echo $V2 1611121.33 1611168.01 printf "%d 1/100th of sec\n" $((161116801-161112133)) 4668 1/100th of sec </code></pre> <p>May take <strong>more</strong> time!</p> <p>If you really wanna keep all in separated file, as @alok suggested, you could <code>source</code> them, as this will not fork:</p> <pre><code>echo &gt;/dev/shm/B3 'read -a V3 &lt;/proc/uptime' . /dev/shm/B3 ;echo $V3 ; for i in {0..10000};do . /dev/shm/B3;done;echo $V3 1612187.89 1612188.79 printf "%d 1/100th of sec\n" $((161218879-161218789)) 90 1/100th of sec </code></pre> <p>This is lot quicker than having to fork for each loop, but accessing to function via filesystem (even RAM pseudo filesystem, without access time) take some time.</p> <h2>My answer</h2> <ol> <li>Yes, it make sense, while <code>$(...)</code> or <code><code>...</code></code> stay usefull for returning result of a binary command or other strong script with a lot of bariables you won't inherit.</li> <li>Depending on your need and who wrote each parts... paralelism... other factors...</li> <li>Other alternatives, for dynamic command, not really, but don't miss to use <em>built-ins</em> like <code>mapfile</code> or <code>read -a</code> when you can.</li> </ol> <h2>Disclaimer</h2> <p>This concern <a href="/questions/tagged/bash" class="post-tag" title="show questions tagged 'bash'" rel="tag">bash</a> only! If you want your script to be <em>portable</em> (even if <code>bash</code> is already ported on most significant os), you have to not use <em>array variables</em> and try your script with <code>zsh</code> or <code>dash</code>.</p>
 

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