Remain in interactive shell after newgrp?

Posted on

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:

  1. $ newgrp specialgroup
  2. $ 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:

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.

Leave a Reply

Your email address will not be published. Required fields are marked *