The POSIX shell built-in newgrp overlays the current shell with /bin/newgrp. The /bin/newgrp command makes the specified group ID number your current group ID and overlays itself with a new shell. You terminate the shell in which you called newgrp by CTRL+D.
Caution!
If newgrp is killed with CTRL+C while /bin/newgrp is replacing the current shell, the shell from which newgrp was called is terminated as well.
The newgrp command can thus be used to switch to another user group. This means:
your access permissions for existing files will be changed to those of the new group affiliation;
group access permissions for new files that you create will be those of the group to which you have switched.
Only the variables that you exported earlier (see export on "export set export attribute for variables") will be known in the (new) current shell after the group has been changed. Variables that have not been exported are either considered undefined or assigned a default value by the shell (see sh on "sh shell, the standard command language interpreter"). Shell variables such as PATH and HOME are also assigned default values, unless they have already been exported by the system or were explicitly exported by you.
Before the call
The /etc/group file must contain an entry for the group that you wish to switch to. Otherwise, newgrp will terminate with an error message.
The newgrp command can be used to switch to any group to which you belong, i.e. any group for which your login name appears in the /etc/group file under the corresponding group entry.
If the error messages "Sorry" and/or "Unknown group" occur then newgrp aborts. In the event of any other error messages the shell terminates.
Syntax
Format 1: | newgrp[ -l][ group] |
Format 2: | newgrp[ -][ group] |
The two formats are described together since the option No argument specified Changes you back to the group whose group ID (GID) has been entered for your login name in the /etc/passwd file.
The newgrp command overlays the current shell with a login shell. Before this shell displays its prompt, it first executes the /etc/profile and your $HOME/.profile (if present) and switches to your home directory. In other words, except for the fact that you are now a member of a new group (the one specified on invoking newgrp), you continue working in the same environment as the one that applied after you logged into the system. – or -l not specified:
Name of the group you wish to switch to. There must be an entry for this group name in the /etc/group file. The associated group ID (GID) must already be associated with a login name in the /etc/passwd file. If you are not a member of the specified group, there must be a password defined for the group in the /etc/group file. newgrp expects you to enter this password before it switches groups. If no password is entered for your login name then a password for the group must be present in the file /etc/group. The command newgrp expects a password to be input before changing to the relevant group. If you wish to change back to the user group which is entered for you, call newgrp without specifying a group name. group not specified: |
Exit status
always 0 |
Error
This name has not been entered in the /etc/group file.
You are not permitted to switch to this group, since you are not a member of the group, and no password has been defined for it either. |
File
/etc/group Defines a name for the entered group IDs and determines all the members of this group. |
Locale
The following environment variables affect the execution of newgrp: LANG Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined. LC_ALL If set to a non-empty string value, override the values of all the other internationalization variables. LC_CTYPE Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments and input files), the classification of characters as upper- to lower-case, and the mapping of characters from one case to the other. LC_MESSAGES Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error. NLSPATH Determine the location of message catalogs for the processing of LC_MESSAGES. |
Example
Change to the group with the group name council:
The new file (newfile) created after the group change is available to members of the council group as a read-only file. |
See also
exec, export |