FactorPad
Faster Learning Tutorials

Linux bash until loop with examples

until allows you to create a program loop within a shell script. (until is technically a bash keyword, not a command)
  1. Purpose - Learn what until is for and how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with until.
  4. A tip - Finish off with one more insight.
Paul Alan Davis, CFA, November 22, 2016
Updated: August 8, 2018
In this tutorial, 99 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 Next

~/ home  / tech  / linux essentials  / until command


The Linux until command (keyword)

Intermediate

Learn to create a program loop in a bash shell script at the Linux command line.

Video Tutorial

Linux bash until loop with examples (3:40)

Videos can also be accessed from the YouTube Playlist.

Video Script

The Command and Why You Need It

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

until allows you to create a program loop (like in a bash shell script).

Recall from videos (tutorials) #96 and #98 on while and for we explored keywords used to create loops, and now we'll see a third example.

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

The until command (keyword) has no traditional options and no arguments as it is technically a keyword, and it too uses do and done syntax, like we've seen before.

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

So why is until an important command (keyword)? Well, until is very similar to while, and it gives you another option for simplifying loop logic. 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 the 3 keywords in this group.

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

All, as you can see, are keywords.

Next, let's create a loop using the same while statement at the command line as we did in video (tutorial) #96. Start by setting the count equals to 1.

Then the logic with while was: while count is less than or equal to 8, print the count, then add one to the count, and head back up until the exit code from the while is not true, thus it repeats.

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

And then last, let's compare. We will get that same result with until. Except here, the exit status is false until it gets to greater than 8, so false 8 times, then the count is above 8 and it breaks out of the loop and hits the done.

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

Very good (remember we will return to our final script in the next tutorial).

A Final Tip

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

One last tip about the until command (keyword). So when do you use until versus while? It can be personal preference, but with experience one or the other will allow you to simplify the code, which really is the goal.

Okay, thanks for visiting today. I hope this was a helpful introduction to the until 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?

If you learned something here, please consider joining our YouTube Channel. Subscribe here.

  • For the Outline to all 100 tutorials, click Outline.
  • To go back to the for statement, hit Back.
  • Stick around to see the exciting finish to the script and the Linux Essentails Playlist with the fold command. Click Next.

Outline Back Next

~/ home  / tech  / linux essentials  / until command



 
 
Keywords:
linux until loop
bash until
bash programming
bash loop
until loop
do until loop
loop until
shell loop
until do done
shell until loop
bash script
bash incremental counter
until keyword
linux until examples
until loop examples
linux until tutorial
shell scripting
do until examples
linux until keyword
linux until statement
bash until statement
linux program iterator