0

I have a few files which needed to be backed up from multiple directories. Few of the files are from the home directory. For example, I need to back up the ~/.ssh/config file. There are multiple files that have to be backed up. The array of files looked like something below.

array=("~/.bash_aliases" "~/.bashrc" "~/.ssh/config" "~/.ssh/set_git_profile.sh")

I looped the array using the for...do...done loop. However, each item in the array was a string and I was unable to resolve ~ with the actual path. For example, ~ to be replaced with /home/<user> in all the file names.

The Final array should look like something below for the user called rahul

array=("/home/rahul/.bash_aliases" "/home/rahul/.bashrc" "/home/rahul/.ssh/config" "/home/rahul/.ssh/set_git_profile.sh")

1 Answers1

0

After some tries and searches, I could not resolve the ~ on the fly, i.e., in the execution of the script. However, there is an easy way of doing this, i.e., using the $HOME shell variable. The $HOME points to the home path of the user. So, finally, I used the below array of files.

array=("${HOME}/.bash_aliases" "${HOME}/.bashrc" "${HOME}/.ssh/config" "${HOME}/.ssh/set_git_profile.sh")