Linux while loop timeout (bash single line) shell script

This works fine (infinite loop):

$ while TRUE; do printf ".";done

.................................................. ...........................

I am trying to timeout this while loopwith the command timeout. All this does not work:

$ timeout 5 while TRUE; do printf ".";done
$ timeout 5 "while TRUE; do printf ".";done"
$ timeout 5 "while TRUE; do printf \".\";done"
$ timeout 5 $(while TRUE; do printf ".";done)
$ timeout 5 $('while TRUE; do printf ".";done')

What is the correct way (if one exists)?

+4
source share
1 answer

I think the solution to your problem is to execute another shell instance and pass the appropriate commands to it. According to the bash manual:

-c        If the -c option is present, then commands are read from the first non-option argument command_string.

So my solution would be something like this:

timeout 5 bash -c -- 'while true; do printf ".";done'

-- , . '' "

+8

Source: https://habr.com/ru/post/1568224/


All Articles