bmk: when I execute the code in command line it isnt responding, but when i put in the script im able to bring it down by pressing ctrl+c. So your suggestion aint working for me.
---------- Post updated at 12:13 PM ---------- Previous update was at 12:12 PM ----------
elixir_sinari: yes off-course we need to call the function.
the function is a recursive one (calls itself) hence you need a if condition in the function body so that it can be terminated when a certain condition is met(e.g when $c reaches a specific value).
bash-3.00$ c=0
bash-3.00$ test_loop() {
> c=$(($c+1))
> echo "count value is : $c "
> sleep 1
> test_loop
> }
bash-3.00$ test_loop
count value is : 1
count value is : 2
count value is : 3
count value is : 4
count value is : 5
^C
count value is : 6
^C
count value is : 7
^C
count value is : 8
^Z
[1]+ Stopped sleep 1
count value is : 9
count value is : 10
count value is : 11
^Z
[2]+ Stopped sleep 1
count value is : 12
count value is : 13
count value is : 14
count value is : 15
count value is : 16
count value is : 17
If you run it on the command line, you are running it directly in the parent shell, so there are no foreground process groups that listen to ctrl-c, except for the "sleep"-command , so each time Ctrl-C is pressed one sleep terminates, but a new sleep gets started by the recursive function loop..
If you run it as a script, then a new shell is started in the foreground in which these commands are run, so then that child shell will listen to CTRL-C and terminate, so then the script stops...
Initially, I was inclined towards a view similar to that of Scrutinizer's. But, when I tried this in bash 4.1, it worked (SIGINT stopped the loop). It also worked in ksh93.
But, when I tried it in bash 3.0 just now, it didn't work (just like the OP had mentioned)!!!
Any explanations anyone?
---------- Post updated at 03:00 AM ---------- Previous update was at 02:24 AM ----------
elixir_sinari:
Finally your answer is much convincing.. I read the link, good explanation. But still I wonder why ctrl-c is ignored since i'm not handling any traps to capture the signals.