0

I want to take file from different directory and pass it to a function as an argument. I have been at this for hours but I can not get it to work. So far I have tried this

#!/bin/bash

myfile=/home/$USER/Desktop/Programs/Files/asd.bam

 function mate()
{ 
    samtools index $arg

    echo "$arg"
 }

mate $myfile

I check argument with echo command but It shows empty.

Thanks in advance.

Saruman
  • 61
  • 1
  • 8
  • 2
    Have you really been searching on this topic? Even a simple [google search](https://www.google.pl/search?q=bash+function+arguments) gives a lot of info? – KamilCuk Apr 09 '19 at 11:41
  • Yes I have and I wrote, I have been trying to get it work for hours. Maybe I am dim-witted or something. – Saruman Apr 09 '19 at 11:44
  • Hm, ok. Just replace `$arg` with `$1`. Ex `echo "$1"`. And it will work. – KamilCuk Apr 09 '19 at 11:50

1 Answers1

1

You haven't defined arg and hence its is empty (undefined). $1 within the function will be the first argument passed.

function mate()
{ 
    arg="$1"
    samtools index "$arg"
    echo "$arg"
}
JRFerguson
  • 7,426
  • 2
  • 32
  • 36