Give a while
loop a boolean condition such as follows:
#!/bin/bash
while true; do
do_something && wait
done
This will execute over and over until bash receives a signal to terminate the process. Usually in the form of ctrl+c
.
You can also use the watch
command to run a script repeatedly as well. For instance a simple clock in your bash terminal using watch
could look like:
$ watch -t -n1 date +%T
The -t
option tells watch
to not display a title of the process it is running. This gives you a clean output of only the command that is being repeated. The -n1
option tells watch to repeat every n
seconds. In this case, -n1
will be intervals of 1
second. Then the date +%T
command shows the time at the time of command completion. Doing this command will give you an active clock in your terminal.
And then one more method that isn't in any of the other answers would be an infinite function call.
do_something() { do_something }; do_something && wait
This is essentially the same as the boolean while
loop only using recursive function calls.
(edit)
For keeping your machine's resources in mind, I have added the && wait
so that each time a process is run, the loops will "wait" until that process finishes with exit status 0 before the next iteration. This is useful in an interactive shell script.