Why does this shell script gradually consume more memory in bash? Im not using any local variables and although the value of cpid is changing at every iteration because ls exits immediately, but it should never grow too large.The shell script in question just monitors a subprocess and checks if its up. This script is named run-alwaysset -euf# monitor a child process# see if it's upcpid=while : ; do    # if cpid isn't set, spawn child    if [ -z $cpid ]; then       $@ &       cpid=$!    fi    # if child isn't active unset cpid    if ps -o pid= -p $cpid; then        :    else        cpid=    fi    sleep 1doneIf I use it with a process that exits immediately (like ls) then we get the following memory usage when I execute the script with dash run-always ls4476kb and it never grows.This is the command Im using to get the memory usage once per second on Linux  theres probably a way to get the memory and peak memory usage directly from ps, but this does the job. while : ; do cat /proc/$(ps a | grep run-always | grep -v grep | awk '{print $1}')/status | grep -i 'vmsize\|vmpeak' ; sleep 1; doneWhen I execute the above script with dash, the memory usage never grows, it hovers at a constant 4476kb on my machine.However, when I use bash (bash run-always ls), every ten seconds or so it allocates another few kilobytes of memory and never frees it.Why is bash doing this?Example output:VmPeak:    16676 kBVmSize:    16676 kBVmPeak:    16676 kBVmSize:    16676 kBVmPeak:    16676 kBVmSize:    16676 kBVmPeak:    16676 kBVmSize:    16676 kBVmPeak:    16676 kBVmSize:    16676 kBVmPeak:    16676 kBVmSize:    16676 kBVmPeak:    16676 kBVmSize:    16676 kBVmPeak:    16676 kBVmSize:    16676 kBVmPeak:    16676 kBVmSize:    16676 kBVmPeak:    16676 kBVmSize:    16676 kBVmPeak:    16676 kBVmSize:    16676 kBVmPeak:    16680 kBVmSize:    16680 kBVmPeak:    16680 kBVmSize:    16680 kBVmPeak:    16680 kBVmSize:    16680 kBVmPeak:    16680 kBVmSize:    16680 kBVmPeak:    16680 kBVmSize:    16680 kBVmPeak:    16680 kBVmSize:    16680 kBVmPeak:    16680 kBVmSize:    16680 kBVmPeak:    16680 kBVmSize:    16680 kBVmPeak:    16680 kBVmSize:    16680 kBVmPeak:    16684 kBVmSize:    16684 kBVmPeak:    16684 kBVmSize:    16684 kBVmPeak:    16684 kBVmSize:    16684 kBVmPeak:    16684 kBVmSize:    16684 kBVmPeak:    16684 kBVmSize:    16684 kBVmPeak:    16684 kBVmSize:    16684 kBVmPeak:    16684 kBVmSize:    16684 kBVmPeak:    16684 kBVmSize:    16684 kBVmPeak:    16684 kBVmSize:    16684 kBVmPeak:    16684 kBVmSize:    16684 kBVmPeak:    16684 kBVmSize:    16684 kBVmPeak:    16684 kBVmSize:    16684 kBVmPeak:    16684 kBVmSize:    16684 kBVmPeak:    16684 kBVmSize:    16684 kBVmPeak:    16688 kBVmSize:    16688 kBVmPeak:    16688 kBVmSize:    16688 kBVmPeak:    16688 kBVmSize:    16688 kBVmPeak:    16688 kBVmSize:    16688 kBVmPeak:    16688 kBVmSize:    16688 kBVmPeak:    16688 kBVmSize:    16688 kBVmPeak:    16688 kBVmSize:    16688 kBVmPeak:    16688 kBVmSize:    16688 kBVmPeak:    16688 kBVmSize:    16688 kBVmPeak:    16688 kBVmSize:    16688 kBVmPeak:    16692 kBVmSize:    16692 kB
Why does bash 4.3 keep allocating memory in a script
bash;dash;memory leaks
As you already discovered that this problem does not appear with dash, it seems to be obvious that there is a high probability that this is caused by a bug.It does also grow if you are using bash-3.x but very slowly - still faster than with other shells.I recommend you to make a bug-report against your version of bash.BTW: I did some tests and there are only two shells where there is no growth at all - regardless how long you wait: mksh and the original Bourne Shell that was not yet converted to use malloc() instead of sbrk().All other shells grow very slowly.