bashsamtools

samtools: command not found in bash script


So I wrote a bash code in which I am using samtools.

#!/bin/bash
# some
# code
samtools view -bS input.sam out.bam

while running this, I get the error :

samtools: command not found.

but I can run samtools fine in the terminal. I looked to the .bashrc file and looks like there is an alias for samtools: alias samtools='samtools_0.1.18' . I added this line at the begining of my code, after #!/bin/bash, but still I get the same error: samtools: command not found. Any Idea how to fix this? Thanks in advance

cross posted here: https://www.biostars.org/p/9602827/#9602830


Solution

  • Aliases are not expanded in scripts (unless the expand_aliases option is set using shopt).

    Instead of an alias, define a function,

    samtools () {
        samtools_0.1.18 "$@"
    }
    samtools view -bS input.sam out.bam
    

    or use the full program name.

    samtools_0.1.18 view -bS input.sam out.bam
    

    If the path to the directory where samtools_0.1.18 is located is not in the $PATH variable, either add it there, or use the full path with the full name.

    Creating a soft link is another option. This creates samtools in your bin directory (I assume you have ~/bin in your $PATH). Just make it point to a new version when you install one.

    ln -s /usr/bin/samtools_0.1.18 ~/bin/samtools