Modifying path variable based on group membership

I’m working on a script that builds Debian binaries for a project that has a reasonably complex build system (c++/Qt). I’ve got my script generating packages with the maintainer scripts that set things up appropriately. Users in a group, created by the maintainer scripts, have access to the /opt directory where the binaries are installed. I’d like to add both desktop shortcuts and additional PATH variables for users in the group created by the maintainer script so it’s easier to execute the binaries. I’ve already tried adding a shell script to the /etc/profile.d directory that in theory should add to the user’s path based on their group membership (this didn’t work and resulted in booting into recovery mode to remove the offending script).

So the multipart question that I’m asking is:
What is considered best practices for achieving what I’d like to achieve?
How do I go about accomplishing this?

That is weird that you had boot problem. The best practices is to let user modify path using their ~/.profile or ~/.bash_profile. Your script can add those to target users directory:

echo 'export PATH=$PATH:/opt/your_app1:/opt/your_app2/bin' >> /home/$USER_NAME/.bash_profile
1 Like

Good question. Is there is a way to detect user membership in script? I need something like this but I have no clue.

@tomboi Look into users/id command. Another option is to query /etc/group file. Is vivek user of group foo?

grep foo /etc/group
groups vivek
id vivek

@tomboi here’s an example I found while doing my research. In theory it should work, as a neophyte with shell script I found it to be hard to implement.

1 Like

@Ian_King good to know. i will try out examples.