This is a test version of Biostars. For the public version, visit https://www.biostars.org.
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://stackoverflow.com/questions/79020546/samtools-command-not-found-in-bash-script

samtools alias

Thank you for your reply. for me, samtools works fine in terminal, but not in the bash code, so I don't think there's anything wrong with path or the installation of command itself.

1 answer

In your terminal, type which samtools. This will give you the full path to samtools. Use that full path in your script. For example:

$ which samtools
/Users/charles/miniforge3/envs/samtools/bin/samtools

For an aliased program like yours, it'll be like this:

$ alias samtools=/Users/charles/miniforge3/envs/samtools/bin/samtools
$ which samtools
samtools: aliased to /Users/charles/miniforge3/envs/samtools/bin/samtools

I'd then use:

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

This is a short term solution. If you want it 'globally' available without this workaround, you should determine where it is installed/located (as per the above), then ensure that location is in your path. For example, in this example:

# assuming for this case that samtools is in /Users/charles/Programs/samtools/bin
echo 'export PATH=${PATH}:/Users/charles/Programs/samtools/bin' >> ~/.bashrc
source ~/.bashrc

Log in to answer this question.