FactorPad
Build a Better Process

Linux bash Shell Script while Loop with Examples

Linux while allows you to create a program loop within a script (while is technically a bash keyword, not a command).
  1. Purpose - Learn what while is for and how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with while.
  4. Script - Add while to our script and run it.
  5. A tip - Finish off with one more insight.
face pic by Paul Alan Davis, CFA
Updated: February 24, 2021
In this tutorial on Linux while, 96 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the video.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / while-command.html


An ad-free and cookie-free website.


Examples of the Linux while Command (Keyword)

Intermediate

Learn to create a while loop at the Linux command line.

Video Tutorial

Videos can also be accessed from the Linux Essentials Playlist on YouTube.

Linux bash Shell Script while Keyword with Examples (3:56)

Video Script

The Command and Why You Need It

Our ninety-sixth word, or command (keyword) to memorize is while from our category Workflow.

while allows you to create a program loop.

Common Linux while Options
-options description
None for the while keyword

Recall from videos (tutorials) #87 to #95, we're using a script to demonstrate workflow, and now we'll introduce while at the command line and then we'll script it.

Before we start, it helps to think of commands as mini programs and most follow this structure: command -option(s) argument(s).

The while command (keyword) has no traditional options and no arguments as it has its own syntax. Also, while is technically a keyword, not a command, and we'll cover 3 related keywords here: while, do and done.

Unlike most commands, help is not available with double-dash --help, as while is a shell built-in covered in the bash manual page.

So why is while an important command (keyword)? Well, for menus to work we need to stay within a script and be offered choices to follow the branches and back. And now you know how to do that.

Demonstration

Okay, the best way to embed this in your memory is by typing in your own terminal window.

Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.

Here we go. Let's review those 3 key words I mentioned in this group.

$ type while; type do; type done while is a shell keyword do is a shell keyword done is a shell keyword

As you can see, they all are keywords.

Next, trying a counting example with a while loop, while then a test block with an expression from our video (tutorial) on test, and then saying while $count is less than or equal to 8, do a print of the count each time, then add 1 to the count and head up to do it again.

$ count=1 $ while [ $count -le 8 ]; do echo $count; count=$((count+1)); done 1 2 3 4 5 6 7 8

Next, let's see while code using nano and this is in our script that we've been working on.

$ nano -c +94 bin/funscript

(Below is the screen from within nano.)

GNU nano 2.2.6 File: /home/factorpad/bin/funscript # From video 96, set up the while statement while true; do clear # From video 94, use if to set up the menu printf "\nPlease select from the following options:\n" printf "\t(1) Show system status\n" printf "\t(2) Collect returns data\n" printf "\t(3) See a sample of the data\n" printf "\t(4) Finish the Linux Essentials playlist\n" printf "\t(5) Quit\n" echo -e "\n Your choice: \c" read -n 1 yourpick if [[ "$yourpick" =~ [1-5] ]]; then if [[ $yourpick == 1 ]]; then printf "\n\tYou Selected 1. One moment please...\n" sleep 2s fun_status continue fi if [[ $yourpick == 2 ]]; then printf "\n\tYou selected 2. One moment please...\n" sleep 2s fun_returns continue fi (other lines in this script trimmed) ^G Get Help ^0 WriteOut ^R Read File ^Y Prev Page ^K Cut Text ^C Cur Pos ^X Exit ^J Justify ^W Where Is ^V Next Page ^U UnCut Text ^T To Spell

The while is here, above the menu but below the functions we addressed in video (tutorial) #95, and while true, do this menu code below, and then within those menu items it points to the functions above.

(Please note: the code above is meant to illustrate the concept of a multi-level conditional statement. It will not run without other code in the script. Go to the last video #100 to see the code for the whole script if you'd like to try it on your own.)

(Hit Ctrl-x to leave nano and y to confirm saving.)

And last, let's run it. Give it a try.

$ ~/bin/funscript The current date and time: Mon Nov 22 21:21:04 PST 2016 What is your name? Paul Thank you Paul (several lines trimmed)

Our first section from the earlier videos shows up first, if you recall. (The section below is after the pause and clear screen.)

Please select from the following options: (1) Show system status (2) Collect returns data (3) See a sample of the data (4) Finish the Linux Essentials playlist (5) Quit Your choice: 5 You selected 5. Thank you, have a nice day. $ _

Then our menu, with the four choices and the fifth leaves the while loop, and exits, right? And then here we'll select that fifth option to quit and then come back later to see these other functions.

A Final Tip

Okay now you know how to use while. And you know the syntax for commands, options and arguments.

One last tip about the while command (keyword). So the takeaway is this: while true, do a block of code, otherwise done.

Okay, thanks for visiting today. I hope this was a helpful introduction to the while command (keyword).


Learn More About The Series

For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:


What's Next?

Don't miss out on free opportunities to learn. Subscribe at YouTube, follow @factorpad on Twitter and join the no-spam email list.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / while-command.html


linux while
bash while
while bash
shell while
bash script
shell script
linux shell script
linux script
bash shell script
while linux
shell script while
bash programming
linux shell script example
bash example
bash while loop
bash do while
bash shell scripting
linux shell script
while loop in shell script
bash while true
linux while loop

A newly-updated free resource. Connect and refer a friend today.