SETENVIRON

Linux specific questions.
jdmcbride
Posts: 28
Joined: Aug 06, 2016 16:13

SETENVIRON

Post by jdmcbride »

in Linux, I can set an environment variable with:

SETENVIRON("COUNT=123")
print ENVIRON("COUNT") will return 123 as a string

so far so good....

Doing this fails:

MYCOUNT="123"

SETENVIRON("COUNT=" + MYCOUNT)
print ENVIRON("COUNT") will return a NULL

Why is that?
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: SETENVIRON

Post by grindstone »

In Windows it works.
Landeel
Posts: 777
Joined: Jan 25, 2007 10:32
Location: Brazil
Contact:

Re: SETENVIRON

Post by Landeel »

This works:

Code: Select all

dim as string MYCOUNT="COUNT=123"
SETENVIRON(MYCOUNT)
print ENVIRON("COUNT")
This fails:

Code: Select all

dim as string MYCOUNT="123"
SETENVIRON("COUNT=" + MYCOUNT)
print ENVIRON("COUNT") 'will return a NULL
Looks like a bug to me.
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: SETENVIRON

Post by fxm »

It works with Windows.

It can't hurt to try this too:

Code: Select all

dim as string MYCOUNT="123"
SETENVIRON("COUNT=" & MYCOUNT)
print ENVIRON("COUNT") 'will return a NULL
Landeel
Posts: 777
Joined: Jan 25, 2007 10:32
Location: Brazil
Contact:

Re: SETENVIRON

Post by Landeel »

fxm wrote:

Code: Select all

dim as string MYCOUNT="123"
SETENVIRON("COUNT=" & MYCOUNT)
print ENVIRON("COUNT") 'will return a NULL
Fails too!
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: SETENVIRON

Post by fxm »

and this?

Code: Select all

dim as string MYCOUNT="123"
SETENVIRON(("COUNT=" + MYCOUNT))
print ENVIRON("COUNT") 'will return a NULL
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: SETENVIRON

Post by badidea »

fxm wrote:and this?

Code: Select all

dim as string MYCOUNT="123"
SETENVIRON(("COUNT=" + MYCOUNT))
print ENVIRON("COUNT") 'will return a NULL
Fail

Code: Select all

dim as string MYCOUNT=""
SETENVIRON("COUNT=123" & MYCOUNT)
print ENVIRON("COUNT")
Fail

Code: Select all

dim as string MYCOUNT=""
SETENVIRON(MYCOUNT & "COUNT=123")
print ENVIRON("COUNT")
Fail

Code: Select all

SETENVIRON("COUNT=123" & "")
print ENVIRON("COUNT")
Pass

FreeBASIC Compiler - Version 1.07.1 (2019-09-27), built for linux-x86 (32bit)
and
FreeBASIC Compiler - Version 1.07.1 (2019-09-27), built for linux-x86_64 (64bit)
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: SETENVIRON

Post by fxm »

Look at SetEnviron troubles

When using 'SETENVIRON(envStr)' with Linux, the 'envStr' string must be permanent (a literal, a variable declared in the main code or a static variable declared in a procedure), because Linux does not memorize the string but only a pointer to its data characters.

Short example:

Code: Select all

dim as string MYCOUNT="123"
dim shared as string envStr1
envStr1 = "COUNT=" + MYCOUNT  '' envStr1 must always exist without ever being modified
SETENVIRON(envStr1)
print ENVIRON("COUNT")
If it works, I would add a note (inside paragraph "Platform Differences") in the documentation.
Last edited by fxm on Oct 22, 2020 9:23, edited 6 times in total.
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: SETENVIRON

Post by badidea »

Ah, that make sense.

Code: Select all

sub set()
	dim as string MYCOUNT="123"
	dim as string envStr1
	envStr1 = "COUNT=" + MYCOUNT  '' envStr1 must always exist without ever being modified
	SETENVIRON(envStr1)
end sub

set()
print ENVIRON("COUNT")
Fail

Code: Select all

dim shared as string envStr1

sub set()
	dim as string MYCOUNT="123"
	envStr1 = "COUNT=" + MYCOUNT  '' envStr1 must always exist without ever being modified
	SETENVIRON(envStr1)
end sub

set()
print ENVIRON("COUNT")
Pass
hhr
Posts: 208
Joined: Nov 29, 2019 10:41

Re: SETENVIRON

Post by hhr »

Code: Select all

Setenviron("COUNT=123")
Print Environ("COUNT")

Const As String MYCOUNT1="456"
Setenviron("COUNT=" + MYCOUNT1)
Print Environ("COUNT")

Dim As String MYCOUNT2="789"
Dim As String envStr="COUNT=" + MYCOUNT2
Setenviron(envStr)
Print Environ("COUNT")

Sleep
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: SETENVIRON

Post by fxm »

'Dim As String envStr' works only when this declaration is done in the main code.
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: SETENVIRON

Post by fxm »

fxm wrote:If it works, I would add a note (inside paragraph "Platform Differences") in the documentation.
Done:
KeyPgSetenviron → fxm [In Linux, 'SetEnviron' memorizes a pointer to the string data characters and not the string itself]
hhr
Posts: 208
Joined: Nov 29, 2019 10:41

Re: SETENVIRON

Post by hhr »

I don't understand 'never be modified':

Code: Select all

Dim As String MYCOUNT,envStr

MYCOUNT="123"
envStr="COUNT=" + MYCOUNT
Setenviron(envStr)
Print Environ("COUNT")

MYCOUNT="ABCDEF"
envStr="COUNT=" + MYCOUNT
Setenviron(envStr)
Print Environ("COUNT")

Sleep
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: SETENVIRON

Post by fxm »

Try this in Linux (I do not have Linux):

Code: Select all

Dim As String MYCOUNT,envStr

MYCOUNT="123"
envStr="COUNT=" + MYCOUNT
Setenviron(envStr)
Print Environ("COUNT")

envStr="modified"
Print Environ("COUNT")

Sleep
I now think that the denomination of "permanent" is sufficient, given the following explanation about memorizing a pointer only?
hhr
Posts: 208
Joined: Nov 29, 2019 10:41

Re: SETENVIRON

Post by hhr »

Thank you. Output in Linux:

Code: Select all

123       Len=3
          Len=0
Post Reply