Skip to content

Potential bug-risk: To redirect both stdout and stderr, 2>&1 must be last (or use '{ cmd > file; } 2>&1' to clarify)

-- DESCRIPTION --

PROBLEMATIC CODE:

firefox 2>&1 (closed) > /dev/null

CORRECT CODE:

firefox > /dev/null 2>&1 (closed)

RATIONALE:

When it comes to redirection, order matters.

The problematic code means "Point stderr to where stdout is currently pointing (the terminal). Then point stdout to /dev/null".

The correct code means "Point stdout to /dev/null. Then point stderr to where stdout is currently pointing (/dev/null)".

In other words, the problematic code hides stdout and shows stderr. The correct code hides both stderr and stdout, which is usually the intention.

EXCEPTIONS:

If you actually do want to redirect stdout to a file, and then turn stderr into the new stdout, you can make this more explicit with braces:

{ firefox > /dev/null; } 2>&1 (closed) Also note that this warning does not trigger when output is piped or captured.

RELATED RESOURCES: https://mywiki.wooledge.org/BashPitfalls#pf43 https://mywiki.wooledge.org/BashFAQ/055

Where to fix it:

To redirect stdout+stderr, 2>&1 (closed) must be last (or use '{ cmd > file; } 2>&1 (closed)' to clarify)

In decpendencies:

scripts/frontend/check_dependencies.sh

check line 4.

Current code:

#!/usr/bin/env bash

if ! yarn check --integrity 2>&1 > /dev/null
then
  echo
  echo "    $(tput setaf 1)yarn check --integrity$(tput sgr0) failed!"
Edited by 🤖 GitLab Bot 🤖