bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: bash exit command should be unconditional


From: Roman Rakus
Subject: Re: bash exit command should be unconditional
Date: Thu, 08 Apr 2010 12:30:51 +0200
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.9) Gecko/20100330 Fedora/3.0.4-1.fc11 Lightning/1.0b2pre Thunderbird/3.0.4

On 04/08/2010 06:49 AM, Bob Proulx wrote:
Vadym Chepkov wrote:
I found out a very unusual feature of bash which makes it to act
really unexpected. I understand that pipelines are executed in a
separate subshell, but I really think 'exit' command should be
absolute. Consider a trivial code:
Note that dash also behaves this way too.

#!/bin/bash

echo Start
ps -ef | while read proc
do
  echo $proc
  exit 1
done
echo Continue

I would expect never see "Continue" printed, I didn't put any
conditional checks to simplify the example, but I really expect the
script to be completely aborted when it gets to 'exit', not having
to add additional checks or replace pipeline with temporary files
This is a variation on Bash FAQ E4.  Pipes create subshells.

You can avoid this by avoiding piping to the while loop.  Instead use
a redirection and no subshell will be created.

   #!/bin/bash
   echo Start
   while read proc
   do
    echo $proc
    exit 1
   done<  <(ps -ef)
   echo Continue

The "<(command)" syntax is documented in the Process Substitution
section of the manual.

Bob


ksh works a bit differently. It also creates subshells, except possibly the last command. Therefor in this case while loop is not subshell and exit exits the whole script.
RR




reply via email to

[Prev in Thread] Current Thread [Next in Thread]