As a follow-up question to the answer proposed in Completely restart Bash -- I'm wondering if this works from within a shell script?
This is for a setup script for new machines.
What I'm doing is replacing a few standard commands with my own variants (e.g. a 'service' variant that provides proper feedback on CentOS 7 instead of a redirect message with no further output) and I'm doing that from a setup.sh script in bash
. These replacements are copied to /usr/local/sbin/
but won't take effect until the shell is restarted. I'm planning to do that with: exec bash -l
as suggested, but does this work from within a shell script?
hash -r
will cause the current shell to forget about any cached paths to commands and try to find them again in$PATH
. If you want to let an unrelated shell do that, you will need its collaboration (eg. have it do atrap 'hash -r' USR1
and kill it withkill -USR1
). – Jun 27 '19 at 08:38/usr/local/sbin
but the existing shell won't pick that up unless the connection is cycled/the shell is restarted. interactively,exec bash -l
replaces the current bash with a new one, but can that be done as part of a script without interrupting the execution of that script? – Mark Straver Jun 27 '19 at 10:03hash -r
. No need forexec bash -l
(I don't see why you should do that, ever, you can also simply. ~/profile
if you have updated it). And you cannot force a shell to do anexec ...
more than you can force it to do ahash -r
(that are ways, like attaching to it with a debugger, or faking it input withioctl(TIOCSTI)
, but they're very slippery and frankly, not worth it). Notice that I'm not telling anything about breaking users' expectations -- most users will NOT appreciate having their shell restarted and all its state wiped off. – Jun 27 '19 at 13:47exec bash -l
replaces the current bash with a new one, sure, but the current bash is the one started to executed the script, not the one that you executed the script from – muru Jun 28 '19 at 12:03