Back to articles
System Software

/dev/null

The Unix bit bucket - a special file that discards everything written to it. Explore how this elegant abstraction works at the kernel level and why it's essential for shell scripting and daemon programming.

kos1
February 2, 2026
8 min read

Introduction

/dev/null is one of Unix's most elegant abstractions - a special file that discards all data written to it and returns EOF on read. Often called the "bit bucket" or "black hole," it's a cornerstone of Unix philosophy and shell scripting.

What is /dev/null?

In Unix-like systems, /dev/null is a character special file (device node) that:

  • Accepts any input - All writes succeed and data is discarded
  • Returns nothing - All reads return EOF immediately
  • Never blocks - Operations complete instantly
  • Has no storage - Uses zero disk space regardless of writes
$ ls -la /dev/null
crw-rw-rw- 1 root root 1, 3 Jan 1 00:00 /dev/null

$ file /dev/null
/dev/null: character special (1/3)

Common Use Cases

The most frequent use is suppressing unwanted output in shell scripts:

# Discard stdout
command > /dev/null

# Discard stderr
command 2> /dev/null

# Discard both stdout and stderr
command > /dev/null 2>&1

# Modern bash syntax (bash 4+)
command &> /dev/null

# Check if command exists (discard output, check exit code)
if command -v gcc > /dev/null 2>&1; then
    echo "GCC is installed"
fi

Redirecting Input

Reading from /dev/null provides an empty input stream:

# Provide empty stdin to a command
command < /dev/null

# Useful for detaching from terminal
nohup ./server < /dev/null > server.log 2>&1 &

# Test how program handles empty input
./my_program < /dev/null

The Kernel Implementation

The kernel implements /dev/null with trivial read and write handlers:

// Simplified Linux kernel implementation (drivers/char/mem.c)

static ssize_t write_null(struct file *file, const char __user *buf,
                          size_t count, loff_t *ppos)
{
    return count;  // Report all bytes "written"
}

static ssize_t read_null(struct file *file, char __user *buf,
                         size_t count, loff_t *ppos)
{
    return 0;  // EOF - zero bytes read
}

static const struct file_operations null_fops = {
    .llseek  = null_lseek,
    .read    = read_null,
    .write   = write_null,
    .splice_write = splice_write_null,
};

Related Special Files

Unix provides several related device files:

  • /dev/zero - Produces infinite stream of null bytes (0x00)
  • /dev/full - Always returns ENOSPC on write (simulates full disk)
  • /dev/random - Produces cryptographically secure random bytes
  • /dev/urandom - Non-blocking random number generator
# Create a 1MB file filled with zeros
dd if=/dev/zero of=zeros.bin bs=1M count=1

# Test error handling for disk full condition
echo "test" > /dev/full
# bash: echo: write error: No space left on device

# Generate random data
head -c 32 /dev/urandom | xxd

Programming with /dev/null

In C, you can use /dev/null like any file:

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

int main() {
    // Silence a file descriptor
    int devnull = open("/dev/null", O_WRONLY);
    dup2(devnull, STDERR_FILENO);  // Redirect stderr to /dev/null
    close(devnull);
    
    fprintf(stderr, "This won't appear\n");
    printf("This will appear\n");
    
    return 0;
}

// Daemonization pattern
void daemonize() {
    // Fork and detach from terminal
    if (fork() > 0) exit(0);
    setsid();
    
    // Redirect standard streams to /dev/null
    int fd = open("/dev/null", O_RDWR);
    dup2(fd, STDIN_FILENO);
    dup2(fd, STDOUT_FILENO);
    dup2(fd, STDERR_FILENO);
    if (fd > 2) close(fd);
}

Performance Considerations

While /dev/null discards data, the write system call still occurs:

# This still generates the data, just discards it
find / -name "*.log" > /dev/null  # Still traverses filesystem

# For truly silent operation in scripts, use conditionals
if [ "$VERBOSE" = "1" ]; then
    command
else
    command > /dev/null 2>&1
fi

# Benchmark write performance
dd if=/dev/zero of=/dev/null bs=1M count=10000
# Shows maximum memory bandwidth, no disk I/O

Cross-Platform Equivalents

Other operating systems have similar concepts:

  • Windows - NUL device (e.g., command > NUL)
  • DOS - NUL device
  • Plan 9 - /dev/null (same as Unix)
  • VMS - NL: null device

Conclusion

/dev/null exemplifies Unix's "everything is a file" philosophy. By treating the bit bucket as a regular file, it integrates seamlessly with existing tools and patterns. Understanding /dev/null and its siblings is essential for shell scripting, daemon programming, and appreciating Unix's elegant design.

Tags

#unix#linux#kernel#shell