in reply to pass perl variable to shell script

Use command substitution in the shell, e.g.

#!/bin/sh output=$(perl -e 'print "hello world"'); echo $output;

If you have a more complex Perl-script then I would put it in it's own file and call it via $(perl script.pl) rathern then embedding it directly in the shell-script as a lot of metacharacters that Perl uses are also special to the shell and escaping all of them properly is a mess.

Replies are listed 'Best First'.
Re^2: pass perl variable to shell script
by arthurs (Novice) on Aug 11, 2010 at 15:38 UTC
    Thanks, morgon. But the command substitution will not work for me here. The code snippet I have is just an example to get my point across on how I can access a variable (str3 in this case) in shell after I set it in Perl.
      The command substuition captures the output of the perl-script at the shell-side.

      So if you only have to pass one value you simply print it to standard output in Perl.

      #in perl-script "script.pl" print $variable; #in the shell-script: variable=$(perl script.pl) echo $variable
      If you must pass more than one variable you e.g. print them in a certain format "$var1=$var2=$var3" and parse that on the shell side.
        morgon, I only have one script -- shell script, in which i have a block of perl code. Lets forget I have 'print' statement in the perl block. Lets say I am setting str3 somewhere in the middle of the perl block. After perl block has finished executing, I am back in the shell part of the code and need to get the content of str3 variable that was set in the perl block of the code. How do I do it?