bash shell io-redirection lock – Does Redirecting Output to a File Apply a Lock on the File?

bashio-redirectionlockshell

If I have a command

$ ./script >> file.log

that gets called twice, with the second call occurring before the first one ends, what happens?

Does the first call get an exclusive lock on the output file? If so, does the second script fail when attempting to write, or does the shell accept the output (allowing the script to end) and throw an error?

Or does the log file get written to twice?

Best Answer

Since you're using >>, which means append, each line of output from each instance will be appended in the order it occurred.

If your script output prints 1\n through 5\n with a one second delay between each and instance two is started 2.5 seconds later you'll get this:

1
2
1
3
2
4
3
5
4
5

So to answer your question: No.