dev-null

Redirecting or appending to /dev/null


>/dev/null or >>/dev/null? I understand the difference when writing (to) a regular file. But when it comes to /dev/null? Comments? Advices?


Solution

  • It seems the behaviour for redirecting to /dev/null via either redirect > or append >> is identical. A quick test shows that it also makes no difference timing wise:

    Content to print:

    for i in range(10**4):
        print("content")
    

    Test time command:

     time python printlots.py >> /dev/null ; time python printlots.py > /dev/null
    

    Result:

    $ time python printlots.py >> /dev/null ; time python printlots.py > /dev/null
    
    real    0m0.094s
    user    0m0.047s
    sys     0m0.047s
    
    real    0m0.096s
    user    0m0.031s
    sys     0m0.063s
    

    So it won't make a measureable difference which you use. It seems the reason both work is to enable developers to use /dev/null in their code with more flexibility. If you have a program where one input parameter is the output file it prints to, and append is your default mode, not having append to /dev/null would mean you'd have to check first what the target file is. At least that's what this answer assumes.