it doesn't work the way you showed it (I added the 3 lines you mentioned right after the last EOF (curly bracket seems unnecessary) and it gave me syntax error at the str3=$... line).

Um. "I did it differently from way you showed it, and it doesn't work."

Why am I not surprised? Why are you surprised?

The "curly bracket" (the parenthesis, actually) closes the opening parenthesis on the 'perl' line, and is indeed necessary - at least if you want the script that I wrote to work.

The core of your question seems to be "can I set a variable in Perl and read it from the shell". The answer is no, you cannot: the Perl instance that you're launching is a child process of the shell. Any variable it sets disappears when the child process exits. The only way to keep the content of that variable is to either save it in some external resource (e.g., a file) or to make it part of the output from the Perl process and capture that output in the shell. The mechanism for doing the latter is called "command substitution" - which my script demonstrates.

One possible (and possibly fraught) exception is if the value that you're trying to pass is a number, 0-255. You could then use that value as the exit status of your script, and read that status via the '$?' variable in the shell. Although this is usable, and in fact used by some standard programs (e.g., "mail"), it's not recommended; that exit flag is normally used to determine whether a program succeeded or failed.


--
"Language shapes the way we think, and determines what we can think about."
-- B. L. Whorf

In reply to Re^3: pass perl variable to shell script by oko1
in thread pass perl variable to shell script by arthurs

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.