The specification
The installer takes two arguments. The first is a source directory that holds dotfiles, which are files whose names start with a dot. The second is a destination directory, normally your home directory. The script must do four things. It must refuse to run when the source directory does not exist, and it must exit with a non-zero status when it does. It must create a symbolic link in the destination for every dotfile in the source. When the destination already has a file or a link with the same name and it does not point to the right source, the script must move that file into a backup directory named .dotfiles-backup inside the destination. Finally, running the script a second time must do nothing harmful: links that are already correct are left alone, and nothing that is already in the backup is overwritten.
Why a backup, and why a plain move
A destructive installer is a common source of lost settings. If the script deletes an old .bashrc, the hours spent tuning it are gone. Moving the file into a backup directory costs nothing and makes the change reversible. The backup is kept in the destination rather than in the source so that it stays with the machine where the change happened.
The script uses mv rather than rm. The move works for regular files, directories, and symbolic links, so the same rule applies to every kind of existing entry. A file name in the backup directory is the same as the original name, which makes it easy to restore by moving it back.
Working through the rules
Most of the logic is a loop over the source files and a single test per file. The test asks whether the destination entry is already a link that resolves to the same source. The readlink -f command resolves symbolic links to their final absolute path, so two different spellings of the same file compare as equal. When the test passes, the loop continues without touching the file. When it fails, the script creates the backup directory, moves the old entry there, and creates the link.
Use set -eu at the top of an installer. The script stops on the first error, and it reports an unset argument instead of silently using an empty path.
The reference solution
The reference script below meets the full specification. It is short, and each line corresponds to a rule. Read it once with the rules in mind, then write your own version before you compare.
#!/bin/bash
# usage: install.sh SRC_DIR DEST_DIR
set -eu
src=${1:?usage: install.sh SRC_DIR DEST_DIR}
dest=${2:?usage: install.sh SRC_DIR DEST_DIR}
[ -d "$src" ] || { echo "no such source directory: $src" >&2; exit 1; }
backup="$dest/.dotfiles-backup"
mkdir -p "$dest"
for f in "$src"/.[!.]*; do
[ -e "$f" ] || continue
name=$(basename "$f")
target="$dest/$name"
if [ -L "$target" ] && [ "$(readlink -f "$target")" = "$(readlink -f "$f")" ]; then continue; fi
if [ -e "$target" ] || [ -L "$target" ]; then mkdir -p "$backup"; mv "$target" "$backup/$name"; fi
ln -s "$(readlink -f "$f")" "$target"
echo "linked $name"
doneThings the script does not do
The installer does not run other programs, does not edit files, and does not read the content of the dotfiles. It only moves and links. That narrow scope is a feature. Installing packages, changing the shell, or writing into a file such as .bashrc belongs in a separate step, where the change is visible. Keeping the installer to linking also means it is easy to audit in a few minutes.
The installer also has no opinion about where the dotfiles repository lives. You can keep it in a Git checkout, in a plain directory, or in a bundle copied from another machine. The script only reads the directory you name, which means the same tool works for all three layouts.
The script also does not handle directories inside the source. A dotfile directory such as .config is linked as a single entry, which is the behavior most people want, but it means the whole directory is shared between the source and the destination.
Testing the installer safely
Test the installer on a throwaway source and destination before you run it on your real home. Create a small source directory with two dotfiles, create a destination with one conflicting file, run the script, and inspect the result. Then run the script a second time. A correct installer prints nothing new on the second run except the links it had to make, and the backup still holds the original file exactly as it was.
Extending the design
Once the basic version works, there are natural extensions. A dry-run flag can print the actions instead of performing them. A restore command can move each backup entry back to its name. A timestamped backup directory keeps several rounds of backups separate. Add each one only when you need it, and test the second run after every change, because idempotence is the property that makes an installer safe to repeat.