Loop Forever Shell Script
For some reason the SCF C application coredumps and the script is exiting. I'm not a shell script expert by any means but my understanding was that it should loop forever regardless of what happens in the SCF app? Do you know any reason why the script would jump out the loop and exit. Thanks Marvin. Powershell Tutorial – Creating an endless loop, avoiding a call depth overflow. Sometimes when scripting you need to create a never ending loop while your script either waits for some other task to complete or repeats a task over and over again. Jul 29, 2017 You can run a shell script in infinite loop by using while loop. #!/bin/bash while true do echo 'Press CTRL+C to stop the script execution' # Enter your desired command in this block. Done You can also do this using below inline command while true; do echo 'Press CTRL+C to stop the script execution'; done You can also run.
I have gotten the following to work:
It produces a bunch of lines of output: 2
, output: 3
, so on.
Shell Script Shebang
However, trying to run the following:
produces the following:
How can I get the compiler to realize it should treat $max as the other end of the array, and not part of a string?
Peter Mortensen11 Answers
Brace expansion, {x..y} is performed before other expansions, so you cannot use that for variable length sequences.
Instead, use the seq 2 $max
method as user mob stated.
So, for your example it would be:
Peter MortensenTry the arithmetic-expression version of for
:
This is available in most versions of bash, and should be Bourne shell (sh) compatible also.
system PAUSEsystem PAUSEStep the loop manually:
If you don’t have to be totally POSIX, you can use the arithmetic for loop:
Or use jot(1) on BSD systems:
Nietzche-jouNietzche-jouIf the seq
command available on your system:
If not, then use poor man's seq
with perl
:
Watch those quote marks.
mobmobThis is a way:
Bash:
The above Bash way will work for ksh
and zsh
too, when bash -c
is replaced with ksh -c
or zsh -c
respectively.
Note: for i in {2..${max}}; do echo $i; done
works in zsh
and ksh
.

Here it worked on Mac OS X.
It includes the example of a BSD date, how to increment and decrement the date also:
Peter MortensenWell, as I didn't have the seq
command installed on my system (Mac OS X v10.6.1 (Snow Leopard)), I ended up using a while
loop instead:
*Shrugs* Whatever works.
Peter MortensenThese all do {1..8}
and should all be POSIX. They also will not break if youput a conditional continue
in the loop. The canonical way:

Another way:
and another:
Use:
You need the explicit 'eval' call to reevaluate the {} after variable substitution.
Peter Mortensenprotected by Brad Larson♦Nov 1 '13 at 17:19
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
Not the answer you're looking for? Browse other questions tagged unixsyntaxshell or ask your own question.
In this chapter, we will discuss shell loop control in Unix. So far you have looked at creating loops and working with loops to accomplish different tasks. Sometimes you need to stop a loop or skip iterations of the loop.
In this chapter, we will learn following two statements that are used to control shell loops−

The break statement
The continue statement
The infinite Loop
All the loops have a limited life and they come out once the condition is false or true depending on the loop.
A loop may continue forever if the required condition is not met. A loop that executes forever without terminating executes for an infinite number of times. For this reason, such loops are called infinite loops.
Example
Here is a simple example that uses the while loop to display the numbers zero to nine −
Empty For Loop In Shell Script
This loop continues forever because a is always greater than or equal to 10 and it is never less than 10.
The break Statement
The break statement is used to terminate the execution of the entire loop, after completing the execution of all of the lines of code up to the break statement. It then steps down to the code following the end of the loop.
Syntax
The following break statement is used to come out of a loop −
The break command can also be used to exit from a nested loop using this format −
Here n specifies the nth enclosing loop to the exit from.
Example
C# Loop Forever
Here is a simple example which shows that loop terminates as soon as a becomes 5 −
Upon execution, you will receive the following result −
Here is a simple example of nested for loop. This script breaks out of both loops if var1 equals 2 and var2 equals 0 −
Live DemoUpon execution, you will receive the following result. In the inner loop, you have a break command with the argument 2. This indicates that if a condition is met you should break out of outer loop and ultimately from the inner loop as well.
The continue statement
The continue statement is similar to the break command, except that it causes the current iteration of the loop to exit, rather than the entire loop.
This statement is useful when an error has occurred but you want to try to execute the next iteration of the loop.
Syntax
Like with the break statement, an integer argument can be given to the continue command to skip commands from nested loops.
Here n specifies the nth enclosing loop to continue from.
Example
The following loop makes use of the continue statement which returns from the continue statement and starts processing the next statement −
Live DemoUpon execution, you will receive the following result −