0

I have a Bash script:

#!/bin/bash
echo this should be visible only in the stderr output but it is not 1>&2
exit 0

Also, I have a custom app that starts this script. The app captures both - stdout and stderr. The stdout is empty and the stderr contains the text provided. So far, so good. But when I start the script from the console (physical terminal):

./my_script.sh

I expect it to not print anything on the display, but it prints. I tried various combinations of redirecting, e.g., >&2, >/dev/stderr, etc. but results are the same. I also tried to redirect stdout to null 1>&2 >/dev/null, but obviously it also sends my text to the null.

Where is this configured? How can I make my terminal to not display stderr?

7
  • 6
    By default, for a terminal command, stdin, stdout and stderr are all connected to the terminal. To discard stderr, script 2>/dev/null works. Commented Oct 22, 2021 at 13:47
  • @Paul_Pedant that’s worthy of being written up as an answer... Commented Oct 22, 2021 at 14:05
  • @Paul_Pedant, I can live with that, but can this be configured somewhere? So I can execute ./script and it automatically disconnects stderr from the terminal?
    – Maris B.
    Commented Oct 22, 2021 at 14:08
  • 4
    Start the script with exec 2>/dev/null.
    – choroba
    Commented Oct 22, 2021 at 14:10
  • 1
    @MarisB. As choroba advised, include exec 2>/dev/null in the very first line of your bash script. In this way you can just execute ./script Commented Oct 22, 2021 at 15:13

2 Answers 2

3

To stop the script from sending its standard error stream to the terminal, you need to redirect it to /dev/null or a file.

You do this by invoking the script with the standard error stream redirected on the command line or letting the script itself redirect its error stream.

To redirect the error stream to /dev/null on the command line, invoke the script using

./my_script 2>/dev/null

If you want the script to discard its standard error stream automatically, then redirect the stream to /dev/null within the script:

#!/bin/bash

# Discard any diagnostic output, errors, and interactive prompts.
exec 2>/dev/null

echo 'This should not be visible.' >&2
echo 'This should be visible.'

Change /dev/null to the pathname of a file to pass the diagnostic output to that file instead.

The standard output stream is not affected by the redirection of the standard error stream to /dev/null or to a file.

Note that a script should not ordinarily be terminated by exit 0.

1
  • About exit 0. There are various script templates with different exit statuses like exit 1, exit 3 etc. I just grabbed one with zero.
    – Maris B.
    Commented Oct 22, 2021 at 15:44
1

To cause this one specific script to discard its standard output:

Given a script:

date
date -r /var/empty/foo

edit it to become a group command. Grouping the commands together in this way permits the standard output and standard error streams of the combined commands to be handled (redirected, piped, discarded, etc.) as though the group was logically a single command.

For example:

{
date
date -r /var/empty/foo
} > /dev/null

will discard the standard output but not alter the routing of the standard error stream.

The following script:

{
date
date -r /var/empty/foo
} > /dev/null 2>&1

will redirect its standard output to /dev/null, and then will redirect its standard error to its standard output, thus discarding both stderr and stdout. That particular syntax is equivalent to:

{
date
date -r /var/empty/foo
} > /dev/null 2>/dev/null

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .