Problem :
For most of my projects my default primary group is correct. However, for one of them I need to set a new primary group before entering its environment in order to avoid permissions issues down the line. Currently this is a two step process:
$ newgrp specialgroup
$ source specialenvironment
I would like to reduce this to a single command. It seems to me that I should be able to do this:
$ sg specialgroup -c "bash --login -c 'source specialenvironment'"
I would think this would create a subshell in which the group is set to ‘specialgroup’, then create another subshell which immediately sources specialenvironment
. However, it doesn’t, so I am clearly mistaken.
Similar questions that have not resolved my issue:
1-how-do-you-use-newgrp-in-a-script-then-stay-in-that-group-when-the-script-exits
2-problem-while-running-newgrp-command-in-script
Any ideas on how to solve this?
Solution :
Add this at the end of your .bashrc
:
[ $(id -g) -eq 1234 ] && source specialenvironment
where 1234
is the GID of specialgroup
; use the right number. This way when you run sole
newgrp specialgroup
specialenvironment
will be sourced automatically.