What does the command (while true; do echo -n 'Infinite Loop' >> ~/output.file; sleep 1; done) & do?

Prepare for the Red Hat Certified Systems Admin Exam EX200. Enhance your skills with interactive quizzes, flashcards, and detailed explanations. Ace your certification exam today!

Multiple Choice

What does the command (while true; do echo -n 'Infinite Loop' >> ~/output.file; sleep 1; done) & do?

Explanation:
The command demonstrates a background, infinite loop that keeps appending text to a file every second. The parentheses run the commands in a subshell, and the trailing ampersand sends that subshell to the background so your shell is free to continue. The loop itself, while true; do ...; done, runs forever because true always succeeds. Inside, it appends the string Infinite Loop to ~/output.file without a newline (because of -n), then waits for one second (sleep 1) before repeating. The redirection >> appends to the file rather than overwriting it, so the file grows with each iteration. So you end up with a background process that writes to the file once every second until you stop it (for example, with kill or by bringing the job to the foreground and interrupting). It won’t print to the screen, and it isn’t a cron job.

The command demonstrates a background, infinite loop that keeps appending text to a file every second. The parentheses run the commands in a subshell, and the trailing ampersand sends that subshell to the background so your shell is free to continue.

The loop itself, while true; do ...; done, runs forever because true always succeeds. Inside, it appends the string Infinite Loop to ~/output.file without a newline (because of -n), then waits for one second (sleep 1) before repeating. The redirection >> appends to the file rather than overwriting it, so the file grows with each iteration.

So you end up with a background process that writes to the file once every second until you stop it (for example, with kill or by bringing the job to the foreground and interrupting). It won’t print to the screen, and it isn’t a cron job.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy