Re: simple question on conditional expression
Andreas Kähäri <[email protected]> Mon, 6 Oct 2025 12:55:13 +0200
| Newsgroups | gmane.comp.shells.zsh.devel,gmane.comp.shells.zsh.user |
|---|---|
| Message-ID | <[email protected]> |
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 .