help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] When to use exec to replace the shell?


From: Bob Proulx
Subject: Re: [Help-bash] When to use exec to replace the shell?
Date: Thu, 28 Jan 2016 14:02:32 +0800
User-agent: Mutt/1.5.24 (2015-08-30)

Peng Yu wrote:
> exec can be used for two cases. One is to replace the shell with a
> given program.
> 
> http://wiki.bash-hackers.org/commands/builtin/exec

Yes.  When used in that way it replaces the current shell with the new one.

> But it is not clear to me beside making wrappers, whether are other
> important use cases of which exec is useful.

As a 99% statement if in these days of higher resource systems that
you never used exec to replace the current program with another one
then probably you are fine and you could not be wrong.

> According to this, it seems that this usage of exec is not very useful
> nowadays unless one is on a computer with limited resources. Is it so?
> 
> http://docstore.mik.ua/orelly/unix/upt/ch45_07.htm

I agree with the O'Reilly author.  For the most part.  Aren't you glad
you are working with larger systems these days?  And so if you never
use exec in this way everything still works and there is nothing bad
to be said about it.

However that isn't to say that there aren't good reasons to still use
exec to replace the current shell.  Wrappers are one place.  When NOT
using exec when you look through the 'ps -efH' listing you will see
each and every process.  This is a good thing to do every so often.
It is possible that you will see some long list of processes that
surprise you for being there.  A wrapper that is purely transient to
set a few environment variables or whatever can justifiably exec the
real program.  Why not?  It saves some noise.  It is arguably the most
correct thing to do.  If you don't it probably won't break anything
however.

Another time is when you explicitly want to replace the current
shell.  For a contrived example thought up on the fly, what if I am
sitting at a shell and want to launch a program that when the program
exits I want my shell to exit?  if I just launch the program then when
it exits my shell prompt is presented.  If I don't want that then if I
exec the program then when the progrma exits the shell exits.  If that
was a console login then the console will present another "Login:"
prompt.  If it was a window in a graphics desktop then the terminal
window will exit and be gone too.  If that is what I want then using
exec is one way to do it.

It is a small point of resource optimization to replace the last
command in a shell script with an exec so that the resources used by
the shell are freed immediately.  Not so important now.  Still a
respectable thing to do.  But just not very important now.  But if the
behavior required is that the current shell is completely gone then
using exec to achieve the required behavior is still the correct thing
to do.

Another example.  I am logging in as a role user.  The shell for the
role user is /bin/sh.  That is a common thing to do for role users.
Or similarly I have used a rescue disk to boot up a non-booting system
and it's rescue shell is /bin/sh.  In these cases /bin/sh is
functional but not very command line typing friendly.  I want to use
bash.  Therefore I type in 'exec bash' at the /bin/sh command line and
the /bin/sh proces is now replaced by bash.  Now when I exit bash it
will be just as if I had exited the original /bin/sh command line
shell.  If I had not used exec then I would eventually need to exit
twice.  One exit for the stacked bash and another exit for the
original /bin/sh.  If I am never going to use it again then I might as
well replace it completely.

Hope that helps,
Bob



reply via email to

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