Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 20 Jan 2000 20:52:18 -0600
From:      Jon Hamilton <hamilton@pobox.com>
To:        cjclark@home.com
Cc:        freebsd-questions@FreeBSD.ORG
Subject:   Re: sh(1) Messing with My Mind 
Message-ID:  <20000121025219.F3AED72@woodstock.monkey.net>
In-Reply-To: Your message of "Thu, 20 Jan 2000 17:55:18 EST." <20000120175518.F72914@cc942873-a.ewndsr1.nj.home.com> 

next in thread | previous in thread | raw e-mail | index | archive | help

In message <20000120175518.F72914@cc942873-a.ewndsr1.nj.home.com>, "Crist J. Cl
ark" wrote:
} On Thu, Jan 20, 2000 at 10:40:08PM +0100, Oliver Fromme wrote:
} > Crist J. Clark <cjc@cc942873-a.ewndsr1.nj.home.com> wrote in list.freebsd-q
} uestions:
} >  > [...]
} >  > seems to have muddled my thoughts this morning. Why does this happen:
} >  > 
} >  > $ echo 3 | read NUM
} >  > $ echo $NUM  
} >  > 
} > 
} > Because the read command is executed in a subshell when it is
} > in a pipe.  When the pipe ends, the subshell terminates, and
} > its environment variables are gone.
} 
} *grumble-grumble*
} 
} I could swear I have done this in the past.

[ use backticks instead ... ]

} That is what I ususally do if the command generating the output is
} short. There are two reasons I do not want to do this,
} 
} (1) The command generating the output is long. (An awk command-line
}     program being fed by another pipe.)
}
} (2) A read would break up the output just the way I want. (The output
}     happens to be a number followed by a date(1)-type string. A 
}     'read NUM DATE' command would break it up exactly how I want.)

If you only need those values for a while (something that you can easily
loop through), you may want to try this:

foo | bar | bletch | while read NUM DATE ; do
  # whatever
  # more whatever
done

and the variables will be in scope for the duration of the while loop.

} And although it does not impact me, there is another reason, and one
} of the best ones, that someone would rather pipe to read than
} backtick,
} 
} (3) The backticked argument cannot have nested backticks.

That's not true, although it can get ugly when you have to start escaping
backticks.  The alternative is to use the $(command) syntax instead of
`command` syntax.

-- 
   Jon Hamilton  
   hamilton@pobox.com



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20000121025219.F3AED72>