So, I'm working on my first bash script, it is to install my dotfiles on a new system,there's a few git clones and this messes up things because it install them in the root's home directory when the script is executed with sudo

There's basically three things that I need to do as root:

  1. Install packages
  2. move some of the fonts that I get with git clone, to /usr/share/fonts ( I could put them in ~/.fonts but I don't want a folder in my home directory for just one font that I use)
  3. Move .vimrc file into root's home directory (I have a specific .vimrc for root)

I could do the steps above manually but the reason I'm doing a script is to avoid doing that.

How can I do this without installing or git cloning everything else in root's home directory ?

Here's the script I have so far.

PS: This is my first script, I hope it doesn't look that bad.


if [ ! -d "$HOME/.dotfiles" ]; then
    echo "============================"
    echo "== Installing dotfiles... =="
    echo "============================"
    git clone --separate-git-dir="$HOME"/.dotfiles https://github.com/username/dotfiles.git my-dotfiles-tmp >/dev/null 2>&1;
    rsync --recursive --verbose --exclude '.git' my-dotfiles-tmp/ "$HOME"/ >/dev/null 2>&1;
    rm --recursive my-dotfiles-tmp >/dev/null 2>&1;
    echo "Can't make install, '$HOME/.dotfiles' folder already exist"

if [ -f "$HOME/.zshrc" ]; then
    mv .zshrc .zshrc.bak

# create necessary directories
mkdir -p $HOME/.config/zsh/plugins/{colored-man-pages,syntax-highlighting,zsh-autosuggestions}

# git clone necessary zsh extensions
git clone https://github.com/ael-code/zsh-colored-man-pages.git ~/.config/zsh/plugins/colored-man-pages >/dev/null 2>&1
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ~/.config/zsh/plugins/syntax-highlighting >/dev/null 2>&1
git clone https://github.com/zsh-users/zsh-autosuggestions.git ~/.config/zsh/plugins/zsh-autosuggestions >/dev/null 2>&1

# neovim set up
sh -c 'curl -fLo "${XDG_DATA_HOME:-$HOME/.local/share}"/nvim/site/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim' >/dev/null 2>&1

function config {
   /usr/bin/git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME $@
config config status.showUntrackedFiles no

# clear terminal

echo "=========="
echo "== Done =="
echo "=========="

# reload zsh
zsh >/dev/null 2>&1
  • If running script as root then $HOME will be root's home. You have to specify which user and then use that users /home.
    – oldfred
    Feb 9, 2021 at 4:11
  • Try putting cd $HOME above your git statements.
    – Ohiovr
    Feb 9, 2021 at 8:18

2 Answers 2


I would use rsync for the commands.

rsync -a user@123.456.789.012:/github.com/ael-code/zsh-colored-man-pages.git ~/.config/zsh/plugins/

You are only doing a single file at a time it is a simple matter. If the place you store the files allows a shell access account so a .ssh/authorized_keys can be created it can be done without a password.

The command above uses the rsync program to make an archival copy of the /github.com/ael-code/zsh-colored-man-pages.git to the directory on the computer it runs on of ~/.config/zsh/plugins/. A straight copy of the file is made check-summed and guaranteed to be byte for byte copy or it fails to copy it with an error.


Created file test.sh with just this:

echo $HOME

fred@z170-focal-k:~$ bash test.sh 
fred@z170-focal-k:~$ sudo bash test.sh
[sudo] password for fred: 

If not clear, then it should be that is is best to always give full paths and not use system variables which may be interpreted differently depending on multiple conditions.


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.