Skip to content

Repository files navigation

  1. 1. Arithmetic
  2. 2. Filesystem-related conditions
  3. 3. Reading input
  4. 4. Elegant endless loop
  5. 5. Modify variable inside a while loop
  6. 6. More patterns & example scripts

macOS and Bash

On macOS, /bin/bash is GNU Bash 3.2 (the default login shell for many accounts is zsh, but these snippets are bash). If you use a newer Bash from Homebrew, run scripts explicitly with bash script.sh or point your shebang at that interpreter.

All examples below are written to work on Bash 3.2 and newer (including the Bash 5.x builds common on Apple Silicon Homebrew setups).


1. Arithmetic

C-style arithmetic in (( ... )) is clear for numeric loops:

#!/usr/bin/env bash
COUNT=0
MAX=5
while(( COUNT < MAX ));doprintf'COUNT=%s\n'"$COUNT"((COUNT++))done

Runnable demo: scripts/arithmetic-count.sh


2. Filesystem-related conditions

Test paths with [ / [[. A common pattern is to wait until another process creates a file, or to pause while a lock file exists:

#!/usr/bin/env bash
READY_FILE="${TMPDIR:-/tmp}/myapp.ready"# Wait until a sentinel file appearswhile [[ !-f"$READY_FILE" ]];do
sleep 1
doneecho"Ready: $READY_FILE exists"
#!/usr/bin/env bash
LOCKFILE="/tmp/myapp.lock"# Hold off while another job owns the lock filewhile [[ -f"$LOCKFILE" ]];do
sleep 1
done

Note: The old snippet while [ -z "$LOCKFILE" ] loops while the variable is empty, not while a path is missing on disk. For filesystem work, prefer -f, -d, -e, etc., on a non-empty path.

Runnable demo: scripts/wait-for-file.sh


3. Reading input

Read lines from stdin. Use -r (raw) so backslashes are not interpreted, and quote the line variable so spaces and glob characters are safe:

#!/usr/bin/env bashwhile IFS= read -r line;doprintf'Line: %s\n'"$line"done< somefile.txt

Process substitution also works on Bash (including macOS /bin/bash 3.2):

#!/usr/bin/env bashwhile IFS= read -r line;doprintf'Line: %s\n'"$line"done<<(grep -v '^#' config.txt)

Runnable demo: scripts/read-lines.sh


4. Elegant endless loop

: is a built-in that does nothing and exits true—handy for while ::

#!/usr/bin/env bashwhile:;doecho"Do something"
sleep 2
done

For a real service loop you usually add sleep, break, or exit on signals. See scripts/endless-with-break.sh for a safe demo that stops after a few iterations.


5. Modify variable inside a while loop

A pipeline runs the right-hand side in a subshell. Assignments to variables there do not affect the parent shell—this catches many people:

#!/usr/bin/env bash# Wrong for updating 'movie' in the parent shell:
movie=""
cut -d '.' -f 2 movies.txt |while IFS= read -r movie_name;docase"$movie_name"in*Kid*) movie="${movie_name# }" ;; # trim leading space from cutesacdoneecho"After pipe loop: '${movie}'"# still empty

Fix: redirect into while instead of piping, or use process substitution so the loop runs in the current shell:

#!/usr/bin/env bash
movie=""while IFS= read -r line;do
movie_name="${line#*. }"# drop leading "123. "case"$movie_name"in*Kid*) movie="$movie_name" ;;
esacdone< movies.txt
echo"After while-read redirect: '$movie'"

Runnable demo: scripts/modify-variable-subshell-demo.sh (also kept at repo root as modify-variable-inside-while-loop.sh).


6. More patterns & example scripts

ScriptIdea
scripts/arithmetic-count.shNumeric while (( ... ))
scripts/wait-for-file.shPoll until a file exists
scripts/read-lines.shwhile read -r over movies.txt
scripts/endless-with-break.shwhile : with break
scripts/read-flags-while.shParse $@ with while + shift
scripts/process-monitor.shPoll until a PID disappears
scripts/modify-variable-subshell-demo.shSubshell vs parent-shell assignment

From the repo root:

/bin/bash scripts/arithmetic-count.sh
/bin/bash scripts/wait-for-file.sh
/bin/bash scripts/read-lines.sh
/bin/bash scripts/endless-with-break.sh
/bin/bash scripts/read-flags-while.sh
/bin/bash scripts/process-monitor.sh
/bin/bash scripts/modify-variable-subshell-demo.sh

These commands use the system Bash on macOS (/bin/bash, 3.2.x). If your bash on PATH is newer, bash scripts/... is fine too.

About

Snippets for while loop. Bash programming

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages