Gentoo Archives: gentoo-user

From: James Rowe <jnrowe@×××××.com>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] 'if echo hello' in .bashrc
Date: Fri, 08 May 2009 18:09:26
Message-Id: 20090508180840.GA25880@ukfsn.org
In Reply to: Re: [gentoo-user] 'if echo hello' in .bashrc by Carlos Hendson
1 * Carlos Hendson (skyclan@×××.net) wrote:
2 > [1] The reason an error message is shown here is because it's bash
3 > that's reporting the broken pipe error. Grep's error message was
4 > redirected to /dev/null, which was:
5 >
6 > grep: unrecognized option '--unsupported'
7 > Usage: grep [OPTION]... PATTERN [FILE]...
8 > Try `grep --help' for more information.
9 >
10 > So even when the system doesn't support --color, that original code will
11 > pollute the screen with bash's error message.
12
13 SIGPIPE behaviour depends on the shell, how it was built and its
14 configuration so won't always receive an error.
15
16 The point of this mail however is that there is still a way around it,
17 just call the commands within a subshell. Compare:
18
19 $ (echo hello | grep --colour l >/dev/null 2>&1) && echo colour support
20 colour support
21 $ (echo hello | grep --broken_arg l >/dev/null 2>&1) && echo broken_arg support
22
23 with the original non-subshell'd version:
24
25 $ echo hello | grep --broken_arg l >/dev/null 2>&1 && echo broken_arg support
26 -bash: echo: write error: Broken pipe
27
28 Thanks,
29
30 James