Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting or appending to /dev/null

Tags:

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?

like image 519
useful Avatar asked Jan 30 '26 22:01

useful


1 Answers

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.

like image 154
jaaq Avatar answered Feb 03 '26 09:02

jaaq



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!