help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Anyone clarify ?


From: Greg Wooledge
Subject: Re: Anyone clarify ?
Date: Wed, 10 May 2023 07:09:28 -0400

> May 9, 2023 at 23:20 by budikusasi_at_gmail.com_63l618u2@duck.com:
> > Anyone clarify what the definitive difference between:
> >
> >  3<&0
> >
> >  exec 3<&0

unicorn:~$ help exec
exec: exec [-cl] [-a name] [command [argument ...]] [redirection ...]
    Replace the shell with the given command.
    
    Execute COMMAND, replacing this shell with the specified program.
    ARGUMENTS become the arguments to COMMAND.  If COMMAND is not specified,
    any redirections take effect in the current shell.

    [...]

So: the first one is just a redirection, and would normally need to be
part of a larger command.  For example:

while read -r -u3 line; do
    process "$line"
done 3<&0

The second one is an actual command, and can be executed as is.  It
will cause the redirection to occur within the current shell, instead
of being temporary for a single command.

In a cleanly written script, there would be an   exec 3<&-   later on,
to close the file descriptor that was opened by the previous exec,
whenever the script is done using it.

exec 3<&0
while read -r -u3 line; do
    process "$line"
done
exec 3<&-

On Tue, May 09, 2023 at 11:26:42PM -0400, 63l618u2@duck.com wrote:
> Exec is a built-in, you might be using a different shell.

Let's assume that a message sent to the help-bash mailing list, using
bash syntax, pertains to bash.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]