# perl. How to set persistent environment variables

**URL:** <https://community.unix.com/t/perl-how-to-set-persistent-environment-variables/266417>\
**Category:** Shell Programming and Scripting\
**Created:** [May 25, 2010, 1:47pm UTC](https://community.unix.com/t/perl-how-to-set-persistent-environment-variables/266417 "2010-05-25T13:47:29Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![MKNENI](https://community.unix.com/letter_avatar/mkneni/32/6_22734026db12803ebb3e059622ac3534.png) [@MKNENI](https://community.unix.com/u/MKNENI)\
**Post date:** [May 25, 2010, 1:47pm UTC](https://community.unix.com/t/perl-how-to-set-persistent-environment-variables/266417/1 "2010-05-25T13:47:29Z")

</div>

I have test.pl scrit with these few lines.

```nohighlight
#!/usr/bin/perl
$ENV{'ORACLE_SID'} = "D3771";
$ENV{'ORACLE_HOME'} = "/oracle/product/10.2.0/db_1";

```

When I try . test.pl it throws an error. When I try test.pl, it doesn't reaing the variables I set in the script.

```nohighlight
[]-> . test.pl
ksh: {ORACLE_SID}: not found
ksh: {ORACLE_HOME}: not found

[]-> test.pl

[]-> echo $ORACLE_SID

[]->

```

Is there a way to get around this issue ?

Thanks for you help

😕😕😕

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_22734026db12803ebb3e059622ac3534.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [May 25, 2010, 1:58pm UTC](https://community.unix.com/t/perl-how-to-set-persistent-environment-variables/266417/2 "2010-05-25T13:58:57Z")

</div>

Hi.

Maybe you should use

```nohighlight
. ./test.pl

```

(assuming it's executable, otherwise make it so with chmod)

Reason is most likely that the current directory (.) is not in your PATH - generally a good thing!

Out of interest, why are you trying to set environment variables using Perl, in this way?

Unless your current (could be your default?) shell is Perl (which is unlikely), this is bound to fail.

---

<div class="post-metadata">

**Author:** ![MKNENI](https://community.unix.com/letter_avatar/mkneni/32/6_22734026db12803ebb3e059622ac3534.png) [@MKNENI](https://community.unix.com/u/MKNENI)\
**Post date:** [May 25, 2010, 2:06pm UTC](https://community.unix.com/t/perl-how-to-set-persistent-environment-variables/266417/3 "2010-05-25T14:06:05Z")

</div>

No luck. I tried with ./ and specifying full path. Getting the same error message.

I need to do a lot of string manipulation before setting these 2 environment variables. I feel more comfortable with perl than shell scripting. That is the reason behind using perl here.

Thanks

---

<div class="post-metadata">

**Author:** ![SFNYC](https://community.unix.com/letter_avatar/sfnyc/32/6_22734026db12803ebb3e059622ac3534.png) [@SFNYC](https://community.unix.com/u/SFNYC)\
**Post date:** [May 25, 2010, 2:13pm UTC](https://community.unix.com/t/perl-how-to-set-persistent-environment-variables/266417/4 "2010-05-25T14:13:18Z")

</div>

Your PERL program inherits its environment from the SHELL that spawned it, changing environment variables will only last for the duration of the PERL program.

At the shell prompt, if you echo $ORACLE\_SID and $ORACLE\_HOME, do you see any values? If you want to set these two environment variables, perhaps you should set them from your profile instead.

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_22734026db12803ebb3e059622ac3534.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [May 25, 2010, 2:16pm UTC](https://community.unix.com/t/perl-how-to-set-persistent-environment-variables/266417/5 "2010-05-25T14:16:02Z")

</div>

> [@mkneni](#):
>
> No luck. I tried with ./ and specifying full path. Getting the same error message.
> 
> I need to do a lot of string manipulation before setting these 2 environment variables. I feel more comfortable with perl than shell scripting. That is the reason behind using perl here.
> 
> Thanks

Great, but you can't source a Perl script into a Bash / Ksh, etc. environment.

Perhaps an alternative, if you're more comfortable in Perl is to do the string manipulation there, and then print the values. Then, the shell can be used to set them.

(excuse my Perl!)

```plaintext
$ cat Perl1
#!/usr/bin/perl
$ENV{'ORACLE_SID'} = "D3771";
$ENV{'ORACLE_HOME'} = "/oracle/product/10.2.0/db_1";

printf( "%s %s\n", $ENV{'ORACLE_SID'}, $ENV{'ORACLE_HOME'} );

$ cat Test1
#!/usr/bin/ksh
./Perl1 | read ORACLE_SID ORACLE_HOME

echo $ORACLE_SID
echo $ORACLE_HOME

$ ./Test1
D3771
/oracle/product/10.2.0/db_1

```

> [@sfnyc](#):
>
> Your PERL program inherits its environment from the SHELL that spawned it, changing environment variables will only last for the duration of the PERL program.
> 
> At the shell prompt, if you echo $ORACLE\_SID and $ORACLE\_HOME, do you see any values? If you want to set these two environment variables, perhaps you should set them from your profile instead.

If you have one Oracle instance (and no global listener to access a remote one) locally then that's fine.

In any case, I always believe that the easiest way to set up an Oracle environment is by using the Oracle-provided oraenv script 🙂

---

<div class="post-metadata">

**Author:** ![MKNENI](https://community.unix.com/letter_avatar/mkneni/32/6_22734026db12803ebb3e059622ac3534.png) [@MKNENI](https://community.unix.com/u/MKNENI)\
**Post date:** [May 25, 2010, 2:18pm UTC](https://community.unix.com/t/perl-how-to-set-persistent-environment-variables/266417/6 "2010-05-25T14:18:43Z")

</div>

echo $ORACLE\_SID and $ORACLE\_HOME is showing blanks.

I can't set this in the .profile because I keep executing the script by passing a parameter and this perl script should set the env variables based on the parameter.

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_22734026db12803ebb3e059622ac3534.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [May 25, 2010, 2:28pm UTC](https://community.unix.com/t/perl-how-to-set-persistent-environment-variables/266417/7 "2010-05-25T14:28:22Z")

</div>

It has to be run inside your current shell in order to successfully set environment variables. This means your perl script will never work -- even if perl runs, your perl interpreter will have its own separate environment and the changes won't happen in your own shell.

Fortunately it's extremely easy to convert this script into a generic shell script.

```nohighlight
export ORACLE_SID="D3771"
export ORACLE_HOME="/oracle/product/10.2.0/db_1"

```

You should be able to source this file fine. It needs no #!/bin/ksh line and does not need to be set executable.

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_22734026db12803ebb3e059622ac3534.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [May 25, 2010, 2:31pm UTC](https://community.unix.com/t/perl-how-to-set-persistent-environment-variables/266417/8 "2010-05-25T14:31:15Z")

</div>

> [@corona688](#):
>
> It has to be run [b]inside[/i] your own shell in order to successfully set environment variables. This means your perl script will never work -- even if perl runs, your perl interpreter will have its own separate environment.
> 
> Fortunately it's extremely easy to convert this script into a generic shell script.
> 
> ```plaintext
> export ORACLE_SID="D3771"
> export ORACLE_HOME="/oracle/product/10.2.0/db_1"
> 
> ```
> 
> You should be able to source this file fine.

It is very easy, but don't overlook the O/P's statement about string manipulation (of which we know nothing :))

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_22734026db12803ebb3e059622ac3534.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [May 25, 2010, 2:35pm UTC](https://community.unix.com/t/perl-how-to-set-persistent-environment-variables/266417/9 "2010-05-25T14:35:47Z")

</div>

> [@mkneni](#):
>
> I can't set this in the .profile because I keep executing the script by passing a parameter and this perl script should set the env variables based on the parameter.

That changes the problem quite a bit.

You will need to have the script execute the necessary program inside it then, so it can export things the way you want them, then launch the program with the updated environment.

```plaintext
#!/bin/ksh

export ORACLE_SID="D3771"
export ORACLE_HOME="/oracle/product/10.2.0/db_1"

exec my_program

```

Or in perl:

```plaintext
#!/usr/bin/perl

export_environment_as_I_please;

system("program");

```

---

<div class="post-metadata">

**Author:** ![dazdseg](https://community.unix.com/letter_avatar/dazdseg/32/6_22734026db12803ebb3e059622ac3534.png) [@dazdseg](https://community.unix.com/u/dazdseg)\
**Post date:** [May 26, 2010, 12:53am UTC](https://community.unix.com/t/perl-how-to-set-persistent-environment-variables/266417/10 "2010-05-26T00:53:11Z")

</div>

do you need to run ur script only with 1 setting. or do u need to change both the variables depending upon some parameters.

---

<div class="post-metadata">

**Author:** ![MKNENI](https://community.unix.com/letter_avatar/mkneni/32/6_22734026db12803ebb3e059622ac3534.png) [@MKNENI](https://community.unix.com/u/MKNENI)\
**Post date:** [May 31, 2010, 3:16am UTC](https://community.unix.com/t/perl-how-to-set-persistent-environment-variables/266417/11 "2010-05-31T03:16:11Z")

</div>

Thanks you guys. Looks like its lot of pain using perl to set evn varialble.

I switched to shell script. That is working good.

Thanks again to everybody.

:b::b:
