AnnaSchumann AnnaSchumann - 7 months ago 18
Perl Question

Passing arguments to perl using bash variables

Ordinarily I would invoke Perl and supply required arguments from within a bash script simply using:

perl script.pl arg1


However there are cases when I want to store both the perl script directory and the arguments in bash variables:

PERLDIR = "/example/directory/script.pl"
ARG1 = "40"


When trying to call the perl script using:

perl "$PERLDIR"


It works, however when trying to provide the argument i'm not sure of the syntax to utilise. If I use:

perl "$PERLDIR $ARG1"
it'll attempt to open the directory:

/example/directory/script.pl 40


And throw an error.

Is there a way to do this and if so, how?

Answer

You should use:

 perl "$PERLDIR" "$ARG1"

When you use many variables in one string enclosed with " it becomes one argument.