Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: simple question on conditional expression



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