I think the best you can do is to run the commands you need to run using
The sg command works similar to newgrp but accepts a command. The
command will be executed with the /bin/sh shell. With most shells you
may run sg from, you need to enclose multi-word commands in quotes.
Another difference between newgrp and sg is that some shells treat
newgrp specially, replacing themselves with a new instance of a shell
that newgrp creates. This doesn't happen with sg, so upon exit from a
sg command you are returned to your previous group ID.
So, after you have created the group and added yourself to it, you will be able to run commands as a member of that group with
sg groupname command. So, for example, this will work:
sudo groupadd --system webapps
sudo useradd --system --gid webapps --home /home/lucio/server/webapps/hello_django hello
sudo chown -R hello:webapps .
sudo chmod -R g+w .
sudo usermod -a -G webapps $(whoami)
## From now on, run all commands through sg
sg webapps "mkdir foorbar"
Since that will be a pain to write, I would put it in a function:
run_as_webapps() sg webapps "$@"
Then, call whatever commands you need to call using that function
run_as_webapps mkdir foobar
And the end of the day though, it might be easier to just run the whole script as root instead. Either that or have two scripts, one to set up the groups and another to do everything else. Just log out and log back in before running the second one.
The POSIX specification clearly states that functions should be defined in this way:
foo () command
For reasons I don't understand, the OP had trouble with this and instead had to use
sg webapps "$@"