Skip to content

Instantly share code, notes, and snippets.

@boltronics
Created January 11, 2022 03:28
Show Gist options
  • Save boltronics/5cf521b53d37112fe7eb8ed7e15c0b44 to your computer and use it in GitHub Desktop.
Save boltronics/5cf521b53d37112fe7eb8ed7e15c0b44 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Add supplementary group permissions and update the shell prompt.
# Requires adding the following to .bashrc after PS1 has been set:
# if [ -n "${PS1_PREFIX}" ]
# then
# export PS1="${PS1_PREFIX}${PS1}"
# unset PS1_PREFIX
# fi
declare newsubgrp="${1}"
if [ -z "${newsubgrp}" ]
then
echo "Usage: $(basename ${0}) GROUP" 1>&2
exit 1
fi
if ! groups | cut -d ' ' -f 2- | grep -q "${newsubgrp}"
then
export NEWSUBGRP_OLDPS1="${PS1}" PS1_PREFIX="[${newsubgrp}] "
sg "${newsubgrp}" "newgrp "$(id -gn)""
PS1="${NEWSUBGRP_OLDPS1}"
unset PS1_PREFIX NEWSUBGRP_OLDPS1 newsubgrp
fi
@boltronics
Copy link
Author

Works like newgrp except adds a user to a subgroup so the user's GID is left untouched.
Usage example:

$ id
uid=1000(boltron) gid=1000(boltron) groups=1000(boltron),50(games)
$ newsubgrp docker
Password: 
[docker] $ id
uid=1000(boltron) gid=1000(boltron) groups=1000(boltron),50(games),963(docker)
[docker] $ 
exit
$ id
uid=1000(boltron) gid=1000(boltron) groups=1000(boltron),50(games)
$ 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment