0

This tiny Shell script called prepareFastaRef.command is supposed to receive a file name I am giving as an argument, generate the path based on it, then run a command on that file. It looks like:

#!/bin/bash

REF="~/Dropbox/phd/fastarefs/$1"
echo "$REF"

samtools faidx "$REF"

I run it from the command line with:

prepareFastaRef.command test.fa

But it does not seem to be able to find the file:

[E::fai_build3_core] Failed to open my file ~/Dropbox/phd/fastarefs/test.fa

The problem is not specific to the programme samtools as I have the same issue with others. And I can confirm 100% the file test.fa is in there.

What I find confusing is that this command alone works perfectly:

samtools faidx ~/Dropbox/phd/fastarefs/off_aa1.fa

So it seems to be an issue with how I pass the path from within the Shell script.

What am I missing?

francoiskroll
  • 791
  • 1
  • 9
  • 21
  • 3
    Duplicate: [Tilde in path doesn't expand to home directory](https://stackoverflow.com/questions/5748216/tilde-in-path-doesnt-expand-to-home-directory) – Benjamin W. Nov 29 '19 at 18:00
  • 2
    Use this for your assignment: `REF=~/Dropbox/phd/fastarefs/"$1"` (Quoting not strictly needed here at all, but good practice in general.) – Benjamin W. Nov 29 '19 at 18:01
  • 2
    To diagnose something like this, you can run your script with `set -x` and you'll see that `~` didn't get expanded. – Benjamin W. Nov 29 '19 at 18:02
  • 2
    Or https://www.shellcheck.net/ would have told you, as well. – Benjamin W. Nov 29 '19 at 18:20

0 Answers0