I am writing some bash scripts to install and configure some programs.
Because the script needs to install packages - I run the scripts as root - which itself is no problem (i.e. I have root privileges etc).
However once the packages are installed I want to then configure the normal user files,ideally I would do this as a regular user, as any config files created would be owned by root

An example program snippet is something like

#! /bin/bash

# Because this statement needs to be run as root - the entire script is 
# also run as root
apt-get install -y tmux

cat << EOF > ~/.tmux.conf
# config stuff

Do I need to manually revert file privileges to regular user, at the end of the script - which is simple for a single file, but could get tricky in some situations.
e.g. add something like

chown $USER:$USER ~/.tmux.conf 
chmod 755 ~/.tmux.conf 

Or is there a better way to handle this - like execute the ~/.tmux.conf statement in a subshell as a regular user?

  • You could write the config to a temporary file and then move it in place with the install command. – jordanm Jul 6 '15 at 23:39
  • thanks, can you elaborate a bit more? does the install command apply regular user permissions to config files? – the_velour_fog Jul 6 '15 at 23:48

I got a working solution thanks to @jordanm pointing me in the right direction.

  • Docs available at info coreutil 'install invocation'

Basically the GNU install command provides alot of options about managing the files as you move them to their final destination.

simply created a temporary file temp.txt, then ran

install temp.txt final/final.txt --owner=regular_user --mode=755
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.