Search code examples
perlvariablesunixquotes

Perl command-line script and shell variables: How to quote?


I have a shell script (csh) calling Perl like this:

set SHELL_VAR = "foo"
set RES = `perl -e 'my $perl_var = uc("$SHELL_VAR"); print "$perl_var\n"'`
echo $RES

I did not manage to use single or double quotes properly, no matter which combination I tried.

How do I properly mix variables in Perl and shell?

Both are starting with $. Double quotes use variable values, but returns

error perl_var: Undefined variable.

in shell. Enclosing a Perl script by single quotes led to an empty result. Escaping like \$perl_var does not succeed either.


Solution

  • I found another solution, you can simply concatenate several expressions, i.e. you can write

    set XYZ = "foo"`date`bar$HOST'gugus'
    

    for example. This is a concatenation of

    foo + `date` + bar + $HOST + gugus
    

    Thus the following works:

    set SHELL_VAR = "foo"
    set RES = `perl -e 'my $perl_var = uc("'$SHELL_VAR'"); print "$perl_var\n"'`
    echo $RES