Bash Script While Loop Menu: 5 Easy Fixes, Avoid Bugs
A bash script while loop with a case statement is the classic way to build a text menu: show the options, read a choice, act, repeat. The old version of this post gave a full menu script with colours and a log through tee . It works when you type correctly at a terminal. This rewrite drives the same script with piped input in Bash 5.2.37 on Debian 13, and finds four bugs: an endless loop when the input ends, a log full of colour codes, a failure under sh , and an unquoted echo . A fixed version follows. Contents How the bash script while loop menu works Bug 1 in the bash script while loop: end of input Bug 2 and 3 of the bash script while loop: colour codes and the shell Bug 4 of the bash script while loop: unquoted variables and the case variable A fixed bash script while loop menu Common mistakes in a bash script while loop menu How to test a bash script while loop before you ship it FAQ How the bash script while loop menu works The script runs while true , prints the menu with ...