ZWcarp has asked for the wisdom of the Perl Monks concerning the following question:

Hey guys, I'm trying to figure out how to pass a bash variable to a perl command line executable.

for i in 1 2 3 4 5 6 7 10 11 12; do perl -lane 'print p${i}. "\t$_"' p${i}.chr7.miRNA.txt; done

The idea is just to have each file be edited to include the "p" number as the first column. The files are set up as p*.chr7.miRNA.txt where the * corresponds to the patient number. Any help would be great! Thanks!

Replies are listed 'Best First'.
Re: bash loop passing to perl command line
by choroba (Cardinal) on Oct 26, 2011 at 20:19 UTC
    Do not enclose the variable to be expanded into single quotes:
    for i in 1 2 3 4 5 6 7 10 11 12 ; do perl -lane 'print p'$i'. "\t$_"' p$i.chr7.miRNA.txt done

      Drop out of single quotes:

      for i in 1 2 3 4 5 6 7 10 11 12 ; do perl -lane'print "p'$i'\t$_"' p$i.chr7.miRNA.txt done

      Or use double quotes: (Don't forget to escape what needs to be escaped.)

      for i in 1 2 3 4 5 6 7 10 11 12 ; do perl -lane"print qq{p$i\\t\$_}" p$i.chr7.miRNA.txt done

      Or pass it as an argument:

      for i in 1 2 3 4 5 6 7 10 11 12 ; do perl -lane'BEGIN { $i = shift(@ARGV); } print "p$i\t$_"' $i p$i.chr7.miRNA.txt done