69

I want to lint a file, and print the stderr (error message), but do not print the stdout (saying the file is OK).

php -l "foo/bar.php"
  • If there are no errors, it prints a "No errors" message to stdout.
  • If there are errors, it prints a detailed message to stderr. I want only this one.

I figure it will be some magic with >&, but I never understood how those work.

What I want is to consume all stdout, keep stderr.

(Sorry if this is dulicate, but I haven't found any question exactly about this)

MightyPork
  • 18,270
  • 10
  • 79
  • 133

3 Answers3

109

Just send the stdout to null:

cmd > /dev/null

This retains stderr, but suppresses stdout.

nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • Does not appear to work with `source`. I see the contents of the file and errors, or neither. Tried all the following with `source someFile`. Following works to surpress the output, but ALSO errors :/ &>/dev/null; >/dev/null 2>&1; /dev/null 2>/dev/null; >/dev/null 2>/dev/null; – rainabba Jun 15 '21 at 17:13
  • @rainabba: it's possible that your command is printing both output and errors to a single stream, in which case you may need to filter the output (e.g. with grep) in order to get what you want. – nneonneo Aug 07 '22 at 05:39
19

You can use:

php -l "foo/bar.php" 2>&1 > /dev/null

i.e. redirect stderr to stdout and stdout to /dev/null in order to get only stderr on your terminal.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    You beatme for just a couple of seconds!! – Raydel Miranda Aug 15 '14 at 17:34
  • 7
    This does not put stderr "on the terminal". It redirects stderr to the stdout of the caller. One of the biggest mistakes people make is conflating "the terminal" with "stdout". The two are different. Never assume stdout is a tty. – William Pursell Aug 15 '14 at 17:37
  • 1
    @WilliamPursell I actually wonder if that's applicable to the original meaning of those terms. – konsolebox Aug 15 '14 at 17:43
  • 3
    @WilliamPursell :) For an average bash user, (and can say, for an mid-level users too) it is _mosty_ the same. So, naming it as _one of the biggest mistake_ is a bit overargued. When I should call something as an biggest mistake than things like: `cd /tnp; rm -rf *` (as root with mistyped /tmp) :). But, youre right: it is an _common mistake_ what sometimes could causes weird output behavior. :) – clt60 Aug 15 '14 at 17:58
0

If there are no errors, it prints a "No errors" message to stdout. If there are errors, it prints a detailed message to stderr. I want only this one.

This would print the error message if one is sent or No error. if empty.

error=$(php -l "foo/bar.php" 2>&1 >/dev/null)
[[ -z $error ]] && error="No error."
echo "$error"
konsolebox
  • 72,135
  • 12
  • 99
  • 105
  • This not only failed for `$(source foobar 2>&1 >/dev/null)`, but it then prints the output a 2nd time with that last echo. I'm looking for a way to `source foobar`, NOT show the contents of foobar, but still output error message (life if the file doesn't exist or can't be read.) – rainabba Jun 15 '21 at 17:16
  • @rainabba Sure. Ask a question. – konsolebox Jun 15 '21 at 17:48