10

As a part of my setups, I often need, in our environements, to use

newgrp voodoo

If I try to put it in the .bashrc file, I get infinite loops.

Any ideas how to get the same functionality automatically loaded in startup?

3 Answers 3

12

There are 2 scripts that will get started once you start bash:

  • .bashrc for interactive shells

  • .bash_profile for login shells

Depending on the way you run the newgrp command one of them will be executed:

  • newgrp - your_group will start a new login shell and thus read .bash_profile

  • newgrp your_group (without the dash) will start a new interactive shell reading .bashrc

If you don't source one file from the other you should be able to circumvent the recursion by choosing the right combination of startup script and newgrp switch

  • newgrp - ... in .bashrc will work

  • newgrp ... in .bash_profile will work

The other two combinations will lead to the recursion you already described. If one of both files sources the other as it is often seen you'll be in trouble anyways.

BTW.: if .bash_profile doesn't exist bash will read the file .profile instead. I just mention it as some experts even manage to source their .bashrc from there.

If neither combination applies to your setup you might have to take resort to shell scripting instead. The environment variable $GROUPS should report the group you are member of, so something like:

[ "$GROUPS" = "200" ] || newgrp your_group

or whatever the id of your voodoo group is might help.

Sign up to request clarification or add additional context in comments.

1 Comment

Looks like input it to the ~/.bash_profile gets the job done. Thanks.
4

You can check the current group id with the command id -gn, then call newgrp only if you aren't in the right group. So your .bashrc could include

# Switch groups, but only if necessary
if [[ `id -gn` != "mygroup" ]]
then
    newgrp mygroup
    exit
fi

Side note, this syntax also works for ksh.

Comments

0

I'm using Ubuntu: This solution may need adjustment on different distros.

The out-of-the-box Ubuntu setup for a new user is to have .profile and .bashrc startup files; in particular note that .bash_profile doesn't exist by default. If that's the case on your system too you should be able to append this to .profile to switch automatically to the desired group on [shell] startup; note that this should be the last line in .profile,

exec newgrp voodoo

Background info: The .profile calls .bashrc. I found that if I call newgrp from the .profile it avoids looping; since it does create a new shell I prefix it with exec to replace the existing shell with the new shell (so no need to exit twice when you finish your bash session) - but also because of the exec it has to be the last line in .profile.

It may also be helpful to review articles etc. describing the various bash startup files; I found the zwischenzugs blog article gives a good overview, where it also becomes clear why the above line should go in .profile (only need to run newgrp once) rather than in .bashrc (would run newgrp when starting child shell - unnecessary since the effective group was set the first time in .profile; also since newgrp itself starts a non-login, interactive shell, it will run .bashrc once more - leading to the infinite loop issue described in the question)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.