Silence Result of Command

When you want to silence the result of a command in a Unix-like shell environment, you can use the > /dev/null or 2> /dev/null redirection techniques, depending on whether you want to suppress standard output (stdout) or standard error (stderr) respectively. Here’s how you can use them:

  • To silence standard output (stdout) of a command, use > /dev/null:

    1
    
    command > /dev/null

    This will discard the normal output of the command.

  • To silence standard error (stderr) of a command, use 2> /dev/null:

    1
    
    command 2> /dev/null

    This will discard error messages and keep only the standard output.

Here’s an example of how you might use these redirections in practice:

1
2
3
4
5
# Silencing standard output (stdout)
ls /nonexistent-directory > /dev/null

# Silencing standard error (stderr)
ls /nonexistent-directory 2> /dev/null

In the first command, the standard output of the ls command is redirected to /dev/null, so you won’t see the list of files and directories in /nonexistent-directory. In the second command, the standard error is redirected to /dev/null, so you won’t see any error messages if the directory doesn’t exist or if there’s a permission issue.

0%