This solution assumes you've simplified your code and that $REF is more dynamic than presented. If that is not the case, skip to the bottom for simpler solutions.
#!/bin/bash
REF="~/Dropbox/phd/fastarefs/$1"
if [ "$REF" = "~/${REF#??}" ]; then
REF="$HOME/${REF#??}"
fi
echo "$REF"
samtools faidx "$REF"
This takes a look at the first two letters of $REF. If they are a tilde and then a slash, that is replaced by $HOME.
With a little more code, you can also implement other home directories like ~root:
#!/bin/bash
REF="~/Dropbox/phd/fastarefs/$1"
# Output the given user's home directory (passwd's 6th colon-delimited field)
user2home() {
getent passwd "$1" |awk -F: '{ print $6 }'
}
case "$REF" in
( ~/* ) REF="$HOME/${REF#??}" ;;
( ~* ) REF="${REF#?}"; REF="$(user2home "${REF%%/*}")/${REF#*/}" ;;
esac
echo "$REF"
samtools faidx "$REF"
These also work with /bin/sh (POSIX-compliant, no bashisms). They employ parameter substitution:
${REF#??} is the contents of $REF without its first two characters (like s/^..//)
${REF#?} is the contents of $REF without its first character (like s/^.//)
${REF%%/*} is the contents of $REF truncated before its first slash (like s:/.*::g)
${REF#*/} is the contents of $REF starting after its first slash (like s:^[^/]*/::)
Therefore, given REF="~root/.bashrc", the second case clause would match and $REF would have its first character removed (~root/.bashrc → root/bashrc), then it would be passed to the user2home function with the later path elements removed (root/bashrc → root). The output of user2home would then have a slash and the rest of the path added to it, concatenating /root + / + .bashrc.
Of course, I'm assuming this is a simplified question and that the prefix to $REF isn't hard-coded as it is in this example. If you're doing that, you might as well reduce all of my code down to a simple one-liner:
#!/bin/bash
REF="$HOME/Dropbox/phd/fastarefs/$1"
echo "$REF"
samtools faidx "$REF"
Another way to solve this, as noted by @BenjaminW, would be to pull the tilde out of the quoted string:
#!/bin/bash
REF=~/"Dropbox/phd/fastarefs/$1"
echo "$REF"
samtools faidx "$REF"
REF=~/Dropbox/phd/fastarefs/"$1"(Quoting not strictly needed here at all, but good practice in general.)set -xand you'll see that~didn't get expanded.