Roy
Roy

Reputation: 743

Transferring variable into a shell function

Is there a way to use shell function that accepts variable from the bash script (or rather, transfer a variable into a shell function)?

The following procedure works just fine (Note, I'm using this procedure as part of my need to implement output redirection as explained here):

mycmd() { cat <(head -3 MyProgrammingBook.txt|awk -F "\t" '{OFS="\t"}{print "Helloworld",$0}') > outputfile.txt; }; 
export -f mycmd; 
bsub -q short "bash -c mycmd"

However, I would like to provide the initial file name as a variable and not as hardcoded name, something such as the following, but the following doesn't work:

myinputfile="MyProgrammingBook.txt";
mycmd() { cat <(head -3 ${myinputfile}|awk -F "\t" '{OFS="\t"}{print "Helloworld",$0}') > outputfile.txt; }; 
export -f mycmd; 
bsub -q short "bash -c mycmd"

Ultimately, mycmd() would be called inside a loop and will be utilized each time with a different variable.

Upvotes: 0

Views: 45

Answers (1)

that other guy
that other guy

Reputation: 123470

export the variable too:

myinputfile="MyProgrammingBook.txt";
mycmd() { cat <(head -3 ${myinputfile}|awk -F "\t" '{OFS="\t"}{print "Helloworld",$0}') > outputfile.txt; }; 
export -f mycmd;
export myinputfile;   # Here
bsub -q short "bash -c mycmd"

Upvotes: 3

Related Questions