Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: simple question on conditional expression
- X-seq: zsh-users 30374
- From: Andreas Kähäri <andreas.kahari@xxxxxx>
- To: Helmut Jarausch <jarausch@xxxxxxxxx>
- Cc: zsh-users@xxxxxxx
- Subject: Re: simple question on conditional expression
- Date: Mon, 6 Oct 2025 12:55:13 +0200
- Archived-at: <https://zsh.org/users/30374>
- In-reply-to: <Z36OAOC3.QG24F7EC.YDGC5QCW@UOEOBWNG.T5YU5OXC.D3VB5HWZ>
- List-id: <zsh-users.zsh.org>
- Mail-followup-to: Helmut Jarausch <jarausch@xxxxxxxxx>, zsh-users@xxxxxxx
- References: <Z36OAOC3.QG24F7EC.YDGC5QCW@UOEOBWNG.T5YU5OXC.D3VB5HWZ>
On Mon, Oct 06, 2025 at 12:24:19PM +0200, Helmut Jarausch wrote:
> Hi,
>
> When I try
>
> ANS='ABC'
> if ( echo $ANS | grep B ); then echo OK;fi
>
> it echoes $ANS.
> How can I disable this?
>
> Many thanks for a hint,
> Helmt
The "if" statement uses the exit status of the command in parentheses to
determine whether to execute the "then" block. The command itself (in
this case, "echo $ANS | grep B") will still output its result to the
terminal unless you redirect it or suppress it.
The output of "grep" may be suppressed by redirecting it to /dev/null,
or (more simply) by using the "-q" (quiet) option:
if echo "$ANS" | grep -q B; then echo OK; fi
or
if grep -q B <<< "$ANS"; then echo OK; fi
--
Matti Andreas Kähäri
Uppsala, Sweden
.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author