Yesterday I got some clarification around using flock
and some simple concepts are becoming clear to me.
My question now is around sub-shells. Take a look at this block
(
flock -s 200
# ... commands executed under lock ...
) 200>/var/lock/mylockfile
My understanding is 200>/var/lock/mylockfile
runs before flock -s 200
, yet if I try something like this
( echo This is a sub-shell ) command
I get an error from BASH
-bash: syntax error near unexpected token `command'
Introducing a semi-colon
( echo This is a sub-shell ); command
fixes the error, but causes command
to run after the sub-shell returns.
So I'm wondering how the initial example causes the redirect to run first. Is it something to do with precedence of the >
over sub-shell (list)
notation? Looking in the BASH manpage I find
Operators are evaluated in order of precedence. Sub-expressions in parentheses are evaluated first and may override the precedence rules above.
200>/var/lock/mylockfile
is, in this context, information about how the subshell should be configured (how its redirection is supposed to be set up). It's not a separate command.
This is true in the same way that
echo "foo" >bar.txt
...always does the redirection first, as does
>bar.txt echo "foo"
In neither of these cases does order matter at all.
A subshell is similar in this respect -- it's just a compound command, and like any other command, it can have redirections specified either at its beginning or its end.