I saw a bash script which had exec 1>&2 command in a function. Something like:
function example()
{
exec 1>&2
cat <<EOT
Script requires at least one parameter.
EOT
exit 1
}
As I understand, exec 1>&2 means that everything from this point one will be directed to stderr. Is this some sort of fixed behaviour of exec one needs to know by heart or is there some good explanation behind this? I mean as I understand, exec in Bash script just invokes a command taking the same PID which the Bash script had and once the command is finished, the PID is killed. 1>&2 isn’t a command. Could somebody explain the details(especially the why question) behind the exec 1>&2 command?
execis a built-in Bash function, so it can have special behavior that an external program couldn’t have. In particular, it has the special behavior that:(That’s quoting from the message given by
help exec.)This applies to any sort of redirection; you can also write, for example, any of these:
(It does not, however, apply to pipes.)