How it works...

Let's understand our script in detail:

  1. Creating the recursive_read_input.sh script is a simple process. We can see that the read command expects input (and will store it in the $input variable), then the script calls recursive_func() again for each time a read exits.

 

  1. Executing the script with $ bash recursive_read_input.sh runs the script indefinitely. No matter the input, Ctrl + C or killing the script will exit it.
  2. Creating the loop_for_input.sh script is relatively trivial as well. We can notice two things: the for loop has no parameters, except for (( ; ; )) and the sleep command. This will make it run forever, but upon each execution of the loop, it will echo Shall run for ever to the console and sleep one second ...

Get Bash Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.