SETENVIRON
SETENVIRON
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?
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?
-
- Posts: 862
- Joined: May 05, 2015 5:35
- Location: Germany
Re: SETENVIRON
In Windows it works.
Re: SETENVIRON
This works:
This fails:
Looks like a bug to me.
Code: Select all
dim as string MYCOUNT="COUNT=123"
SETENVIRON(MYCOUNT)
print ENVIRON("COUNT")
Code: Select all
dim as string MYCOUNT="123"
SETENVIRON("COUNT=" + MYCOUNT)
print ENVIRON("COUNT") 'will return a NULL
Re: SETENVIRON
It works with Windows.
It can't hurt to try this too:
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
Re: SETENVIRON
Fails too!fxm wrote:Code: Select all
dim as string MYCOUNT="123" SETENVIRON("COUNT=" & MYCOUNT) print ENVIRON("COUNT") 'will return a NULL
Re: SETENVIRON
and this?
Code: Select all
dim as string MYCOUNT="123"
SETENVIRON(("COUNT=" + MYCOUNT))
print ENVIRON("COUNT") 'will return a NULL
Re: SETENVIRON
Failfxm wrote:and this?Code: Select all
dim as string MYCOUNT="123" SETENVIRON(("COUNT=" + MYCOUNT)) print ENVIRON("COUNT") 'will return a NULL
Code: Select all
dim as string MYCOUNT=""
SETENVIRON("COUNT=123" & MYCOUNT)
print ENVIRON("COUNT")
Code: Select all
dim as string MYCOUNT=""
SETENVIRON(MYCOUNT & "COUNT=123")
print ENVIRON("COUNT")
Code: Select all
SETENVIRON("COUNT=123" & "")
print ENVIRON("COUNT")
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)
Re: SETENVIRON
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:
If it works, I would add a note (inside paragraph "Platform Differences") in the documentation.
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")
Last edited by fxm on Oct 22, 2020 9:23, edited 6 times in total.
Re: SETENVIRON
Ah, that make sense.
Fail
Pass
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")
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")
Re: SETENVIRON
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
Re: SETENVIRON
'Dim As String envStr' works only when this declaration is done in the main code.
Re: SETENVIRON
Done:fxm wrote:If it works, I would add a note (inside paragraph "Platform Differences") in the documentation.
KeyPgSetenviron → fxm [In Linux, 'SetEnviron' memorizes a pointer to the string data characters and not the string itself]
Re: SETENVIRON
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
Re: SETENVIRON
Try this in Linux (I do not have Linux):
I now think that the denomination of "permanent" is sufficient, given the following explanation about memorizing a pointer only?
Code: Select all
Dim As String MYCOUNT,envStr
MYCOUNT="123"
envStr="COUNT=" + MYCOUNT
Setenviron(envStr)
Print Environ("COUNT")
envStr="modified"
Print Environ("COUNT")
Sleep
Re: SETENVIRON
Thank you. Output in Linux:
Code: Select all
123 Len=3
Len=0