Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 08-17-2009, 04:45 PM   #1
LQ Newbie
Registered: Jun 2009
Location: Iowa City, IA
Distribution: SuSE 11, ubuntu Hardy
Posts: 28

Rep: Reputation: 15
always true while loop

Hey guys-
Here's a real easy one for you. I need to make a while loop in bash that's always true. I will manually break out of it later on in the script. I thought one of these would work, but the script never enters the loop for some reason:

while (1)

while [1]

Old 08-17-2009, 04:50 PM   #2
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
So what's the body of your loop?
while true; do   date;   sleep 5; done                                                                                        
Tue Aug 18 08:49:35 UTC 2009
Tue Aug 18 08:49:40 UTC 2009

Old 08-17-2009, 05:40 PM   #3
Gentoo support team
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,074

Rep: Reputation: 386Reputation: 386Reputation: 386Reputation: 386
In bash there's a command called ":", which is always evaluated to true, you could use that to shorten it even more (however I advice to use "true" if you are writing this into a script if only for the sake of readability).

$ while :; do echo foo; sleep 5; done
You could also use any other command that you are sure will end without error (true). In this case you could use this, for example:

$ while sleep 5; do echo foo; done
Whose effect will be slightly different, I bet you can guess what the difference is


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 04:49 PM
Is it true? sycamorex General 3 01-08-2009 06:28 AM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 04:50 AM
converting shell while loop to for loop farkus888 Programming 8 09-12-2007 03:30 AM
for loop only works properly on first loop symo0009 Programming 1 12-25-2005 06:17 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:45 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration