Hi Bash Experts!
Every couple of years I stumble upon weird behavior in bash scripts, which I cannot explain, and every time it was due to documented bash behavior. Can, please, someone explain to me why the following script terminates?
I've tried this on Ubuntu 22.04 and Ubuntu 20.04 with same results -- see "Observations" at the end of the script.
Thanks!
=== #!/bin/bash
set -euf -o pipefail
print_test () { local count="$1" while [ $count -ge 0 ]; do if [ x"$(echo a)" = x"a" ]; then echo a fi count=$(($count - 1)) done }
main () { local count=1 while print_test "$count" | grep -q "a"; do echo "$count: OK" count=$(($count + 1)) done echo "$count: BAD" }
main exit 1
# Observations: # 1. With "if" in print_test BAD happens on around count "2" # 2. Without "if" in print_test BAD happens around count "80" # 3. Changing "grep -q" to "grep" makes the problem go away. # 4. Removing -o pipefail makes the problem go away. # 5. While loop in "main" isn't required, but makes reproduction easier. ===
-- Maxim Kuvyrkov https://www.linaro.org