The following link recommends against using loops in shells.    Why is this? Here is an example loop I just happened to be looking at when I came across that answer:  find /etc/postinstall -name '*.sh' | while read script do   echo Running $script ...   $script   mv $script $script.done done
114302
Is using a  to process text generally considered bad practice in POSIX shells?  As , some of the reasons for not using shell loop are conceptual, reliability, legibility, performance and security.  This  explains the reliability and legibility aspects:  while IFS= read -r line &lt;&amp;3; do   printf '%s\n' "$line" done 3&lt; "$InputFile"   For performance, the while loop and  are tremendously slow when reading from a file or a pipe, because the  reads one character at a time.  How about conceptual and security aspects?
115679
Am I correct to assume that when ; joins two commands on a line, Bash always waits until the first command has exited before executing the second command? And similarly, in a shell script containing two different commands on different lines, Bash always waits until the command on the first line has exited before executing the command on the second line?  If this is the case, is there a way to execute two commands in one line or in a script, so that the second command doesn't wait until the first command has finished?  Also, are different lines in a shell script equivalent to separate lines joined by ; or &amp;&amp;?
18003
I was wondering if the following two ways of running a bash script are equivalent?  . ./myScript.sh  source myScript.sh  Are they both running the content of the script instead of running the script, i.e. not creating a subshell for running the script?
15664
I have a text file named links.txt which looks like this  link1 link2 link3   I want to loop through this file line by line and perform an operation on every line. I know I can do this using while loop but since I am learning, I thought to use a for loop. I actually used command substitution like this   a=$(cat links.txt)   Then used the loop like this  for i in $a; do ###something###;done   Also I can do something like this  for i in $(cat links.txt); do ###something###; done   Now my question is when I substituted the cat command output in a variable a, the new line characters between link1 link2 and link3 are removed and is replaced by spaces  echo $a   outputs  link1 link2 link3  and then I used the for loop. Is it always that a new line is replaced by space when we do a command substitution??  Regards
90858
I am trying to do the following in my script:  #/bin/bash -x var1=www var2=www2 var3=www3 var4=www4 for i in 1 2 3 4; do    echo \$var$i done   Obviously, this doesn't work as expected and results in the following output:  $var1 $var2 $var3 $var4   How can I dynamically reference the variables in my loop?
12724
Why do I get different values for $x from the snippets below?  #!/bin/bash  x=1 echo fred &gt; junk ; while read var ; do x=55 ; done &lt; junk echo x=$x  #    x=55 .. I'd expect this result  x=1 cat junk | while read var ; do x=55 ; done echo x=$x  #    x=1 .. but why?  x=1 echo fred | while read var ; do x=55 ; done echo x=$x  #    x=1  .. but why?
7192
Does bash and sh execute a script differently? I executed a shell script with both bash and sh, and got the same output. So what is the difference?  Also, when you just run ./executable, does it use bash or sh?
31397
What can you do with the eval command? Why is it useful? Is it some kind of a built-in function in bash? There is no man page for it..
101873
I need to use shell variables in my gnuplot commands, for which I'm using the here document style. I also need to use loops inside the gnuplot code. Both these things are working.  Now -- I want to use a gnuplot for loop's index to access a shell array variable. This is where I've been stuck all day.  I have something like this:  for ((i=0; i&lt;=10; i++)) do var[$i] = i*10 done  gnuplot&lt;&lt;EOF do for [j=1:10]{ #need to access ${var[j]} somehow val=sprintf("${var[%d]", j) ##doesn't work } EOF   Individual access like ${var[1]} works. I suspect this may need using backquotes and/or expr, but am not sure.
63089
I need to indirectly reference a variable in the bash shell.  I basically want to what you can do in make by writing $($(var)).  I have tried using ${$var} which would be the most straight forward solution in bash but then I get this error:  bash: ${$var}: bad substitution   Is there a way to do this?  What I am trying to do is to iterate over all the arguments ($1, $2, $3, ...) to a program using an iteration variable and I cannot do this without indirection.
3220
I am wondering what the point of sh being symbolic linked to dash is? I understand that dash is supposed to be faster than bash, but I am uncertain why the original sh shell isn't present in sh.   Or if anything why isn't sh linked to bash?
44920
What are the fundamental differences between the mainstream *NIX shells and what scenarios might prompt you to use one over the other?  I understand that some of it probably comes down to user preference but I've only ever used Bash and I'm interested to hear where another shell might be useful. Also, is there an impact on user-written shell scripts when running under one shell or another or is it simply a matter of changing the shell at the top of the file?  My instinct says it's not that easy.
65652
In bash, I know that it is possible to write a for loop in which some loop control variable i iterates over specified integers.  For example, I can write a bash shell script that prints the integers between 1 and 10: #!/bin/bash  for i in {1..10} do  echo $i done  Is it possible to instead iterate over a loop control variable that is a string, if I provide a list of strings?  For example, suppose that I have a string fname that represents a file name.  I want to call a set of commands for each file name.  For example, I might want to print the contents of fname using a command like this: #!/bin/bash  for fname in {&quot;a.txt&quot;, &quot;b.txt&quot;, &quot;c.txt&quot;} do  echo $fname done  In other words, on the first iteration, fname should have the value fname=&quot;a.txt&quot;, while on the second iteration, fname should have the value fname=&quot;b.txt&quot;, and so on.  Unfortunately, it seems that the above syntax is not quite correct.  I would like to obtain the output:  a.txt b.txt c.txt  but when I try the above code, I obtain this output:  {a.txt, b.txt, c.txt}  Can you please help me determine the correct syntax, so that I can iteratively change the value/contents of the variable fname?  Thank you for your time.
94875
I want to print list of numbers from 1 to 100 and I use a for loop like the following:  number=100 for num in {1..$number} do   echo $num done   When I execute the command it only prints {1..100} and not the list of number from 1 to 100.
82839
Why is it that using bash and suspending a while loop, the loop stops after being resumed? Short example below.  $ while true; do echo .; sleep 1; done . . ^Z [1]+  Stopped                 sleep 1 $ fg sleep 1 $   I'm familiar with signals, and I'm guessing this may be the natural behaviour of bash here, but I'd like to better understand why it behaves in this particular way.
22243
I am having difficulty getting a for loop to take user input in the form of an argument to a function I created. #!/bin/bash number1=&quot;$1&quot;    #Assume user input 5 loops=&quot;$2&quot;      #Assume user input 5 if [[ ${number1} &amp;&amp; ${loops} =~ ^-?[0.00-9.99]+$ ]];then    for number2 in {1..${loops}}    do        Product=$(echo &quot;${number1} * ${number2}&quot; | bc -l)        echo &quot;${number1} * ${number2} = ${Product}&quot;        if test ${number2} -eq 7;then            { echo &quot;----------------------&quot;; echo &quot;${number2} is the best number.&quot;; echo &quot;----------------------&quot;; continue; }        elif test ${number2} -eq 11;then            { echo &quot;------------------------------&quot;; echo &quot;${number2} is pretty awesome as well.&quot;; echo &quot;------------------------------&quot;; continue; }            fi     done else    { echo &quot;Please try again with a valid numerical entry.&quot;; exit 1; } fi  This script does not run when the user inputs an integer for the variable &quot;loops&quot;, and any number for &quot;number1&quot;. number1 = 5 in this case. It returns the error:  (standard_in) 1: syntax error 5 * {1..5} = /home/$USER/For_Loop_Multiplication.sh: line 9: test: {1..5}: integer expression expected /home/$USER/For_Loop_Multiplication.sh: line 11: test: {1..5}: integer expression expected  If I instead directly input an end bound on the number of loops directly into the script like the following, and number1 = 5, it runs fine: #!/bin/bash number1=&quot;$1&quot;     #Assume user input 5 if [[ ${number1} =~ ^-?[0.00-9.99]+$ ]];then    for number2 in {1..5}    do        Product=$(echo &quot;${number1} * ${number2}&quot; | bc -l)        echo &quot;${number1} * ${number2} = ${Product}&quot;        if test ${number2} -eq 7;then            { echo &quot;----------------------&quot;; echo &quot;${number2} is the best number.&quot;; echo &quot;----------------------&quot;; continue; }        elif test ${number2} -eq 11;then            { echo &quot;------------------------------&quot;; echo &quot;${number2} is pretty awesome as well.&quot;; echo &quot;------------------------------&quot;; continue; }            fi     done else    { echo &quot;Please try again with a valid numerical entry.&quot;; exit 1; } fi  It returns the result:  5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25  Essentially what I think is happening is that the script seems to be placing the entire range as the value number2, which is defined as the iterator in the for loop. Since the input for the variable &quot;loops&quot; is an integer, there should be no difference between asking the user for a range and placing it in the loop, or simply setting it as a default when I write the script. However, there apparently is a difference... Any insight into solving this problem is appreciated. Thanks.
20165
I have written a simple script.  When I runsh &lt;myscriptname.sh&gt;, i got the correct output, but when I run ./&lt;myscriptname.sh&gt;, I got an error.  What is difference between when I do sh and ./?