I have a large bash script that is supposed to install all dependencies for an application and then build the application itself.
I run the script with sudo because most of the commands require it, e. g.:
apt update
apt install -y libunwind8
apt install -y curl
...
Then it gets to building the application:
npm install
npm run build
When the script gets to these two commands it fails, giving me the following error:
/home/crispjam/.npm/_cacache/tmp/git-clone-98eb9fb8/.git: Permission denied
I did some research on this and found out that when interacting with a git repository you shouldn't use sudo.
I tried running the npm install outside of the script and it did, indeed, work, suggesting that sudo makes that command fail inside the script.
I've considered adding sudo to all the commands inside instead of running the script with sudo but I've read here that for the most part it is not considered good practice.
In the top answer I read that you can drop the sudo privileges from a single command by prepending sudo -u username to it.
Is this good practice?
And how can I make this dynamic so that instead of using my username the script uses the name of the user running the script?