I have a shell script in which I am running perl script by below code.

perl perlscript.pl

In the perl script I have defined a variable called $circle. Now I want to use this variable value in my shell script. How can I call?

  • 2
    Does your Perl script produce any other output to stdout? – roaima May 7 '16 at 8:41
  • You can use perl code to update your PATH and set an environment variable. – Niklas R. May 7 '16 at 8:58
  • please provide more details. e.g. sample output from your perlscript.pl and maybe the script itself (or a minimal versison of it). – cas May 7 '16 at 10:09
  • Put your perl scritp into your post. – PersianGulf May 7 '16 at 13:43
  • @Programmer400, you cannot. You can alter the variables while the perl script is running but once it exits, whatever it changed will exit along with it. – glenn jackman May 8 '16 at 1:06

If your perl script produces no other output than the value of $circle, you can use command substitution to store that output in a variable. For example:

circle=$(perl perlscript.pl)

If the perl script produces other output as well (or not output at all), you'll have to either:

  1. extract only the value you want from the output using the usual text processing tools (sed, awk, perl, grep, etc). Here's a very simple example:

    circle=$(perl perlscript.pl | sed -e 's/junk.i.dont.want//')

  2. use an indirect method, such as having the perl script write the value of $circle to a file (e.g. /path/to/circle) for your shell to read it (e.g. circle=$(cat /path/to/circle))

NOTE: Without more details from you, it's impossible to provide more than generic advice like this.


Just set an environment variable in perl and your shell will find it. In C you can check your path like this

void getPath() {
    if (getenv("PATH") == NULL) {
        printf("'%s' is not set.\n", "PATH");
        /* Default our path if it is not set. */
    else if (getenv("PATH")) {
        printf("'%s' is set to %s.\n", "PATH", (getenv("PATH")));

There are also functions for setting your path, so you can set an environment variable for your program in your code.

  • 2
    child processes (e.g. a perl script) can't change the parent's (e.g. the shell that the perl script was run from) environment. – cas May 7 '16 at 9:36
  • @cas But you can have 2 shells at once can't I? – Niklas R. May 7 '16 at 9:54
  • 3
    yes, but a child process still can't change its parent's environment. or the environment of an entirely unrelated process (e.g. a 2nd shell). it can affect the environment of itself and any child processes it creates. – cas May 7 '16 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.