The question may be trivial but with this simple for loop  for i in {1..10000000}; do echo "$i" line &gt;&gt; file done   my shell crashed after some elaboration time (the tab in the terminal emulator was automatically closed) and file was not created.  It is GNU bash, version 4.1.11(2)-release.  Is this due to a memory limit or what else? And if yes which should be this limit?  With {1..1000000} it worked correctly.
121136
IINM my system is failing when bashing  for i in {0..10000000}; #   Seven zeroes. do     false; done #   `bash` exited and its `tmux` pane/window was closed.   or  for i in $(seq 0 10000000); #   Seven zeroes. do     false; done #   `bash` exited and its `tmux` pane/window was closed.   but not when  for i in {0..1000000}; #   Six zeroes. do     false; done #   Finished correctly.   Can you please briefly explain the internals of this behavior and prompt a workaround for getting the task done?
53320
Is it equivalent to have commands print to a file directly, as opposed to writing to a file descriptor?  Illustration  Writing to file directly:  for i in {1..1000}; do &gt;&gt;x echo "$i"; done   Using an fd:  exec 3&gt;&amp;1 1&gt;x for i in {1..1000}; do echo "$i"; done exec 1&gt;&amp;3 3&gt;&amp;-     Is the latter one more efficient?
124066
After going through the bash ,  question and  one it's still not clear to me how can I perform atomic write (append) operations to a file in bash. I have a script that is run in multiple instances and at some point must write data to a file:  echo "$RESULT" &gt;&gt; `pwd`/$TEMP_DIR/$OUT_FILE   How is it possible to make all write operations from all concurrently running scripts to that file atomic (so that data from one instance doesn't overlap data from another)?
96825
Say I have the following script:  #!/bin/bash for i in $(seq 1000) do     cp /etc/passwd tmp     cat tmp | head -1 | head -1 | head -1 &gt; tmp  #this is the key line     cat tmp done   On the key line, I read and write the same file tmp which sometimes fails.  (I read it is because of race conditions because the processes in the pipeline are executed in parallel, which I do not understand why - each head needs to take the data from the previous one, doesn't it? This is NOT my main question, but you can answer it as well.)  When I run the script, it outputs about 200 lines. Is there any way I can force this script to output always 0 lines (so the I/O redirection to tmp is always prepared first and so the data is always destroyed)? To be clear, I mean changing the system settings, not this script.  Thanks for your ideas.
19378
One of my friends wanted to have more logs in the /var/log/ directory, and after some time of using the system, he tried to access the folder and list it, but instead he got the following error:  bash: /bin/rm: Argument list too long   Does anyone know how many files can be added to this rm list?
4873
I'm on windows 7 using Cygwin.  My script and text file are located in the same directory.  #!/bin/bash while read name; do echo "Name read from file - $name" done &lt; /home/Matt/servers.txt   I get this error and I don't know why because this is correct while loop syntax..?  u0146121@U0146121-TPD-A ~/Matt $ ./script.sh ./script.sh: line 4: syntax error near unexpected token `done' ./script.sh: line 4: `done &lt; /home/Matt/servers.txt'   Can anybody tell me what I'm doing wrong? I think its because I'm on windows and using Cygwin.
72672
I get how a normal fork bomb works, but I don't really understand why the &amp; at the end of the common bash fork bomb is required and why these scripts behave differently:  :(){ (:) | (:) }; :   and   :(){ : | :&amp; }; :   The former causes a cpu usage spike before throwing me back to the login screen. The latter instead just causes my system to freeze up, forcing me to hard reboot. Why is that? Both continually create new processes, so why does the system behave differently?   Both of the scripts also behave differently from  :(){ : | : }; :   which doesn't cause any problems at all, even though I would have expected them to be alike. The bash manual page states that the commands in a pipeline are already executed in a subshell, so I'm led to believe that : | : should already suffice. I belive &amp; should just run the pipeline in a new subshell, but why does that change so much?  Edit: Using htop and limiting the amount of processes, I was able to see that the first variant creates an actual tree of processes, the second variant creates all the processes on the same level and the last variant doesn't seem to create any processes at all. This confuses me even more, but maybe it helps somehow?
57258
I have been trying to parallelize the following script, specifically each of the three FOR loop instances, using GNU Parallel but haven't been able to. The 4 commands contained within the FOR loop run in series, each loop taking around 10 minutes.   #!/bin/bash  kar='KAR5' runList='run2 run3 run4' mkdir normFunc for run in $runList do    fsl5.0-flirt -in $kar"deformed.nii.gz" -ref normtemp.nii.gz -omat $run".norm1.mat" -bins 256 -cost corratio -searchrx -90 90 -searchry -90 90 -searchrz -90 90 -dof 12    fsl5.0-flirt -in $run".poststats.nii.gz" -ref $kar"deformed.nii.gz" -omat $run".norm2.mat" -bins 256 -cost corratio -searchrx -90 90 -searchry -90 90 -searchrz -90 90 -dof 12    fsl5.0-convert_xfm -concat $run".norm1.mat" -omat $run".norm.mat" $run".norm2.mat"   fsl5.0-flirt -in $run".poststats.nii.gz" -ref normtemp.nii.gz -out $PWD/normFunc/$run".norm.nii.gz" -applyxfm -init $run".norm.mat" -interp trilinear    rm -f *.mat done
55899
I have written a small script to count number of occurrences of logs in a particular file. The following is my code:   #!/bin/sh count=1 while read line do     found=$( grep M $line )     OCCURENCE=$( grep M /log.txt)     if [ "$OCCURENCE" ]; then         echo $count")" $line &gt;&gt; filesave.txt     fi     count=$((count+1)) done &lt; log1.txt   but I am getting this error:  ./count.sh ./count.sh: syntax error at line 10: `found=$' unexpected
30227
I was under the impression that the maximum length of a single argument was not the problem here so much as the total size of the overall argument array plus the size of the environment, which is limited to ARG_MAX. Thus I thought that something like the following would succeed:  env_size=$(cat /proc/$$/environ | wc -c) (( arg_size = $(getconf ARG_MAX) - $env_size - 100 )) /bin/echo $(tr -dc [:alnum:] &lt;/dev/urandom | head -c $arg_size) &gt;/dev/null   With the - 100 being more than enough to account for the difference between the size of the environment in the shell and the echo process. Instead I got the error:  bash: /bin/echo: Argument list too long   After playing around for a while, I found that the maximum  was a full hex order of magnitude smaller:  /bin/echo \   $(tr -dc [:alnum:] &lt;/dev/urandom | head -c $(($(getconf ARG_MAX)/16-1))) \   &gt;/dev/null   When the minus one is removed, the error returns. Seemingly the maximum for a single argument is actually ARG_MAX/16 and the -1 accounts for the null byte placed at the end of the string in the argument array.  Another issue is that when the argument is repeated, the total size of the argument array can be closer to ARG_MAX, but still not quite there:  args=( $(tr -dc [:alnum:] &lt;/dev/urandom | head -c $(($(getconf ARG_MAX)/16-1))) ) for x in {1..14}; do   args+=( ${args[0]} ) done  /bin/echo "${args[@]}" "${args[0]:6534}" &gt;/dev/null   Using "${args[0]:6533}" here makes the last argument 1 byte longer and gives the Argument list too long error. This difference is unlikely to be accounted for by the size of the environment given:  $ cat /proc/$$/environ | wc -c 1045   Questions:   Is this correct behaviour, or is there a bug somewhere? If not, is this behaviour documented anywhere? Is there another parameter which defines the maximum for a single argument? Is this behaviour limited to Linux (or even particular versions of such)? What accounts for the additional ~5KB discrepancy between the actual maximum size of the argument array plus the approximate size of the environment and ARG_MAX?   Additional info:  uname -a Linux graeme-rock 3.13-1-amd64 #1 SMP Debian 3.13.5-1 (2014-03-04) x86_64 GNU/Linux
16024
I was working on a shell script and I accidentally created a file with the variable as its name. Now I have $file in my ls output, and cannot remove it.  What can I do?
92427
I have an unexpected behaviour- following snippet fail with 'unbound variable' error:  #!/bin/bash set -u &lt;&lt;EOF a=a b=$a EOF   Tested on:  GNU bash, version 4.4.12(1)-release  GNU bash, version 4.2.46(2)-release
102465
Ok, I know that in Bash (by default, without 'lastpipe' bash option enabled) every variable assigned after a pipeline is, indeed, executed in a subshell and the variable itself dies after subshell execution. It does not remain available to the parent process. But doing some tests, I came up with this behavior:  A) Second command (a=2) assign the value and is returned:  [root@centos01]# a=1; a=2; a=10 | echo $a 2   B) Third command (a=10) assign the value and is returned:  [root@centos01]# a=1; a=2; a=10; a=20 | echo $a 10   C) Fourth command (a=20) assign the value and is returned:  [root@centos01]# a=1; a=2; a=10; a=20; touch fileA.txt | echo $a 20   So:   Why always the last variable assignment in command's sequence is not actually executed? (or if it is, why it is not being caught by the subshell and returned back by echo command?) In test C, the 'touch' command actually created the file 'fileA.txt' in the directory. So, why the last command in the sequence for variable assignment made in step A and set B did not work? Does anyone know the technical explanation for that?
50819
So I'd like to pass the first 512 bytes of binaryFile.dd as the second parameter to myProgram but bash strips out all the NUL chars. Is there any way to avoid this in bash or am I on a hiding to nothing?   myProgram parameter1 "$(head -c 512 binaryFile.dd)"
32103
I want to generate a sorted list with all 8-digit numbers — from 00000000 to 99999999. I typed in the shell:  f() {  while IFS="" read -r line; do    for i in {0..9}; do         echo "$line$i";    done;  done }  echo | f | f | f | f | f | f | f | f | tee result.txt | wc -l   response is     bash: echo: write error: Interrupted system call bash: echo: write error: Interrupted system call bash: echo: write error: Interrupted system call 99998890   Why have I got these three errors and malformed result.txt ?  I use  GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu)  Debian GNU/Linux 9.6 (stretch)  Linux kernel: 4.19.0 #2 SMP Thu Nov 1 15:31:34 EET 2018 x86_64 GNU/Linux
57945
cat &gt;run_pos2bed3.sh &lt;&lt;EOF ls 2*/peaks.txt | while read id; do echo $id done; EOF   after input it  nano only show  ls 2*/peaks.txt | while read id; do echo  done; EOF   How can I add $id after EOF?
121723
I'm having an issue with the following piece of (G)AWK script:  do {     ... } while (system("sleep 10"))   My intention is to break the loop when the user presses ^C during the sleep, but it's not working.  I believe the problem is that Bash exits with 0 when interrupted with ^C, at least when it's executed by AWK's system():  $ awk 'BEGIN { print "\n" system("sleep 2") }' (let the sleep complete) 0      $ awk 'BEGIN { print "\n" system("sleep 2") }' ^C 0   Why is it so?   Is this a bug in Bash or in (G)AWK?  Is there a simple solution that doesn't involve complicated Bash-specific syntax like trap?  The best I could come up with is this:  do {     ... } while (42 == system("sleep 10 &amp;&amp; exit 42"))   which still looks like a kludge to me.