I’d like to create a new group in a script, assign the current user to it, reload the user’s groups and continue executing commands that require the new group.
In this related question I found nice ways of avoiding spawning a new inner shell that I would eventually have to exit out of to get to the current one where the script is running. But the problem is that exec
replaces the current shell altogether, so the script halts. For example:
#!/bin/bash
exec sg GROUP newgrp $(id -gn)
echo hello
Here the script switches the group alright and avoids needing a double exit (say via SSH), but echo
is never run.
I could run a different script with sg
with the commands that require that group and then have the final group update at the end of the script just to be kind to the user, but that seems suboptimal. Is there a way to update group assignments cleanly while running a script?