zsh-workers
 help / color / mirror / code / Atom feed
From: Klaus Alexander Seistrup <klaus@seistrup.dk>
To: Zsh Workers <zsh-workers@zsh.org>
Subject: BUG: Shell builtin `which` prints non-existent commands to stdout
Date: Mon, 24 Sep 2018 10:00:31 +0200	[thread overview]
Message-ID: <20180924080031.ee7lqmthxpmvqaal@klaus.seistrup.dk> (raw)

TL;DR: When `which` does not find a command it prints the error to 
standard output instead of standard error. Exit code is set correctly, 
though, and error messages about ‘bad options’ are sent to standard 
error as expected.

In a nutshell:

```sh
$ echo $SHELL              # can you tell us a little about yourself?
/usr/bin/zsh               # ✓
$ type which               # please tell us what you know about `which`
which is a shell builtin   # ✓
$ which vi                 # can you find `vi`?
/usr/bin/vi                # ✓
$ echo $?                  # show us the exit code
0                          # ✓
$ which nxcmd              # tell me about a non-existent command
nxcmd not found            # ✓
$ echo $?                  # and let us see the exit code
1                          # ✓
$ which nxcmd 2>/dev/null  # this command ought to keep `which` silent, but …
nxcmd not found            # ✗
$ which nxcmd >/dev/null   # … the error message is sent to stdout
$                          # ✗
$ which -T                 # let's try an unrecognized option
which: bad option: -T      # ✓
$ which -T 2>/dev/null     # redirect the same to stderr
$                          # ✓
```

Expected behaviour, as illustrated by GNU `which`:

```sh
$ /usr/bin/which --version | head -1
GNU which v2.21, Copyright (C) 1999 - 2015 Carlo Wood.
$ /usr/bin/which nxcmd 2>/dev/null
$ echo $?
1
```

Interim solution:

```sh
# ~/.zshrc
alias which='command which'
```

OS, architecture and zsh version:

```sh
$ uname -a
Linux nakamu 4.18.9 #1 SMP PREEMPT Wed Sep 19 21:19:17 UTC 2018 x86_64 GNU/Linux
$ echo $ZSH_VERSION
5.6.2
```

Cheers,

-- 
Klaus Alexander Seistrup
https://klaus.seistrup.dk/

             reply	other threads:[~2018-09-24  8:00 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20180924080113epcas4p4f8f89aa03a2cebc5030fd45dca0f6e84@epcas4p4.samsung.com>
2018-09-24  8:00 ` Klaus Alexander Seistrup [this message]
2018-09-24 10:22   ` Peter Stephenson
2018-09-24 12:29     ` Klaus Alexander Seistrup
2018-09-24 12:51       ` Peter Stephenson
2018-09-24 13:22         ` [PATCH] zshbuiltins(1): Document 'which''s "not found is not an error" behaviour Daniel Shahaf
2018-09-24 22:18         ` BUG: Shell builtin `which` prints non-existent commands to stdout Stephane Chazelas
2018-09-25  7:56           ` Stephane Chazelas
2018-09-24 22:32         ` Stephane Chazelas
2018-09-25  9:10           ` Peter Stephenson
     [not found]   ` <20180924112218.7bac7f2c@camnpupstephen.cam.scsc.local>
2018-09-24 10:25     ` Peter Stephenson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180924080031.ee7lqmthxpmvqaal@klaus.seistrup.dk \
    --to=klaus@seistrup.dk \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).