Continue script execution after exec in bash

589 Views Asked by At

I have such code:

echo 'test'
usr=$USER
sudo sh -c "exec su $usr"
echo 'test1'

And for example echo 'test' is code where I configuring something that requirs me to relogin in new shell. But then where code is echo 'test1' I need to continue configuring using reloaded new shell.

Is there way to do that automatically? Like start new shell in parallel or something like that?

Ubuntu 14.04, bash.

Update:

For example, I need to install virsh but after installation it requires sudo to run. My script configurs groups adding $USER to the libvirtd group. Then I need to relogin. I can do that with sudo sh -c "exec su $usr". After that I need the script to continue execution. Is there way to do that?

1

There are 1 best solutions below

1
On BEST ANSWER

Change with visudo your sudoers configuration so that you are no longer asked for a password. See man visudo and man sudoers.

I suggest to use a heredoc:

echo 'test'
usr="$USER"
sudo su - "$USER" << EOF
echo 'test1'
EOF