fbpx while Command in Linux: Creating Conditional Loops | Certificação Linux

Linux while Command: Creating Conditional Loops in Shell Scripts

📅 01/01/2019 ⏱ 1 min ✍️ Uira Ribeiro
Linuxcommands
Linux while Command: Creating Conditional Loops in Shell Scripts
Exam Pass App — mais de 7.000 questões LPI, offline, em 5 idiomas, disponível no Android e na App Store
Available on
Google Play
Available on
App Store

The shell has structures to test conditions and execute certain program sequences multiple times (loop), until the tested condition is satisfied.

The while command in Linux continuously tests an expression, until an expression has a return variable equal to zero or true.

In this example, the variable $i gets the initial value equal to zero.

While (while) the condition of the test command represented by [  ] returns zero (true), the loop will continue to be executed.

In the loop the variable $i is incremented, until $i reaches the condition of not being less than 4:

$ i=0
$ while [ $i -lt 4 ]; do echo "conta $i" ; i=$[$i+1]; doneconta 0conta 1conta 2conta 3

The while needs to have an exit condition satisfied, otherwise it will stay in infinite loop.

Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.

Did you like it? Share

Uira Ribeiro

Prof. Uirá Ribeiro

Chair do Board do Linux Professional Institute

After years of watching professionals waste hours on simple tasks because they didn't know the right Linux tools, I decided to teach.
Today I'm the author of most of LPI's official Learning Materials and mentor to over 14,000 certified students.

See my full story →

That was just one piece. The course teaches the whole path — from your first command to exam day.

Certificação Linux — a plataforma líder em certificações Linux no Brasil
WhatsApp