* Can system modify an environment variable in the current environment?
@ 2021-09-02 19:49 Mortimer Cladwell
2021-09-02 20:06 ` Roel Janssen
2021-09-02 20:09 ` Taylan Kammer
0 siblings, 2 replies; 4+ messages in thread
From: Mortimer Cladwell @ 2021-09-02 19:49 UTC (permalink / raw)
To: guile-user
Hi,
Consider my file test.scm:
(define (main args)
(let* ((myvar (string-append "export
GUILE_LOAD_PATH=/some/random/text:$GUILE_LOAD_PATH"))
(statement1 (system (string-append "echo " myvar " >>
$HOME/.bashrc")))
(statement2 (system myvar)))
(write myvar)))
At the terminal:
mbc@HP8300:~/temp$ echo $GUILE_LOAD_PATH
/home/mbc/.guix-profile/share/guile/site/3.0
mbc@HP8300:~/temp$ guile -e main -s test.scm
"export GUILE_LOAD_PATH=/some/random/text:$GUILE_LOAD_PATH"
mbc@HP8300:~/temp$ echo $GUILE_LOAD_PATH
/home/mbc/.guix-profile/share/guile/site/3.0
At the end of .bashrc I see the last line is:
export
GUILE_LOAD_PATH=/some/random/text:/home/mbc/.guix-profile/share/guile/site/3.0
If I close and reopen the terminal and:
mbc@HP8300:~$ echo $GUILE_LOAD_PATH
/some/random/text:/home/mbc/.guix-profile/share/guile/site/3.0
So statement1 works as expected, modifying .bashrc which is then effective
in modifying GUILE_LOAD_PATH on future invocations of terminal.
statement2 is an attempt to modify the current running environment, but
fails.
Note that if I paste export
GUILE_LOAD_PATH=/some/random/text:$GUILE_LOAD_PATH directly into the
terminal, that successfully modifies the variable.
Why does (system myvar) i.e. (system "export
GUILE_LOAD_PATH=/some/random/text:$GUILE_LOAD_PATH") fail?
Thanks
Mortimer
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Can system modify an environment variable in the current environment?
2021-09-02 19:49 Can system modify an environment variable in the current environment? Mortimer Cladwell
@ 2021-09-02 20:06 ` Roel Janssen
2021-09-19 6:45 ` adriano
2021-09-02 20:09 ` Taylan Kammer
1 sibling, 1 reply; 4+ messages in thread
From: Roel Janssen @ 2021-09-02 20:06 UTC (permalink / raw)
To: Mortimer Cladwell, guile-user
Dear Mortimer,
On Thu, 2021-09-02 at 15:49 -0400, Mortimer Cladwell wrote:
> Hi,
>
> Consider my file test.scm:
>
> (define (main args)
> (let* ((myvar (string-append "export
> GUILE_LOAD_PATH=/some/random/text:$GUILE_LOAD_PATH"))
> (statement1 (system (string-append "echo " myvar " >>
> $HOME/.bashrc")))
> (statement2 (system myvar)))
> (write myvar)))
>
> At the terminal:
>
> mbc@HP8300:~/temp$ echo $GUILE_LOAD_PATH
> /home/mbc/.guix-profile/share/guile/site/3.0
>
> mbc@HP8300:~/temp$ guile -e main -s test.scm
> "export GUILE_LOAD_PATH=/some/random/text:$GUILE_LOAD_PATH"
>
> mbc@HP8300:~/temp$ echo $GUILE_LOAD_PATH
> /home/mbc/.guix-profile/share/guile/site/3.0
>
> At the end of .bashrc I see the last line is:
>
> export
> GUILE_LOAD_PATH=/some/random/text:/home/mbc/.guix-
> profile/share/guile/site/3.0
>
> If I close and reopen the terminal and:
>
> mbc@HP8300:~$ echo $GUILE_LOAD_PATH
> /some/random/text:/home/mbc/.guix-profile/share/guile/site/3.0
>
> So statement1 works as expected, modifying .bashrc which is then
> effective
> in modifying GUILE_LOAD_PATH on future invocations of terminal.
>
> statement2 is an attempt to modify the current running environment, but
> fails.
> Note that if I paste export
> GUILE_LOAD_PATH=/some/random/text:$GUILE_LOAD_PATH directly into the
> terminal, that successfully modifies the variable.
>
> Why does (system myvar) i.e. (system "export
> GUILE_LOAD_PATH=/some/random/text:$GUILE_LOAD_PATH") fail?
>
The "system" call executes in a separate environment which is exited
when the call finishes.
Instead you want to use the "setenv" procedure instead:
(setenv "GUILE_LOAD_PATH" (string-append "/some/random/text:" (getenv
"GUILE_LOAD_PATH")))
Or in this particular case, use the "add-to-load-path" procedure:
https://www.gnu.org/software/guile/manual/html_node/Load-Paths.html
Kind regards,
Roel Janssen
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Can system modify an environment variable in the current environment?
2021-09-02 19:49 Can system modify an environment variable in the current environment? Mortimer Cladwell
2021-09-02 20:06 ` Roel Janssen
@ 2021-09-02 20:09 ` Taylan Kammer
1 sibling, 0 replies; 4+ messages in thread
From: Taylan Kammer @ 2021-09-02 20:09 UTC (permalink / raw)
To: Mortimer Cladwell, guile-user
On 02.09.2021 21:49, Mortimer Cladwell wrote:
> Hi,
>
> Consider my file test.scm:
>
> (define (main args)
> (let* ((myvar (string-append "export
> GUILE_LOAD_PATH=/some/random/text:$GUILE_LOAD_PATH"))
> (statement1 (system (string-append "echo " myvar " >>
> $HOME/.bashrc")))
> (statement2 (system myvar)))
> (write myvar)))
>
> At the terminal:
>
> mbc@HP8300:~/temp$ echo $GUILE_LOAD_PATH
> /home/mbc/.guix-profile/share/guile/site/3.0
>
> mbc@HP8300:~/temp$ guile -e main -s test.scm
> "export GUILE_LOAD_PATH=/some/random/text:$GUILE_LOAD_PATH"
>
> mbc@HP8300:~/temp$ echo $GUILE_LOAD_PATH
> /home/mbc/.guix-profile/share/guile/site/3.0
>
> At the end of .bashrc I see the last line is:
>
> export
> GUILE_LOAD_PATH=/some/random/text:/home/mbc/.guix-profile/share/guile/site/3.0
>
> If I close and reopen the terminal and:
>
> mbc@HP8300:~$ echo $GUILE_LOAD_PATH
> /some/random/text:/home/mbc/.guix-profile/share/guile/site/3.0
>
> So statement1 works as expected, modifying .bashrc which is then effective
> in modifying GUILE_LOAD_PATH on future invocations of terminal.
>
> statement2 is an attempt to modify the current running environment, but
> fails.
> Note that if I paste export
> GUILE_LOAD_PATH=/some/random/text:$GUILE_LOAD_PATH directly into the
> terminal, that successfully modifies the variable.
>
> Why does (system myvar) i.e. (system "export
> GUILE_LOAD_PATH=/some/random/text:$GUILE_LOAD_PATH") fail?
> Thanks
> Mortimer
>
Hi Mortimer,
The "system" procedure starts a child-process in the operating system.
In the Unix process model, the changes to the environment made by a child
process do not affect the parent.
Note that anything for which you use "system" above can be done directly
within Guile by using the appropriate procedures, instead of starting a
child process using the system shell.
You might want to look for I/O related procedures to figure out how you
can append text to an existing file. This might help:
https://www.gnu.org/software/guile/manual/html_node/File-Ports.html
Setting an environment variable is simple, just use 'setenv':
https://www.gnu.org/software/guile/manual/html_node/Runtime-Environment.html
Hope that helps. :-)
--
Taylan
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Can system modify an environment variable in the current environment?
2021-09-02 20:06 ` Roel Janssen
@ 2021-09-19 6:45 ` adriano
0 siblings, 0 replies; 4+ messages in thread
From: adriano @ 2021-09-19 6:45 UTC (permalink / raw)
To: guile-user; +Cc: Mortimer Cladwell
Il giorno gio, 02/09/2021 alle 22.06 +0200, Roel Janssen ha scritto:
>
> Or in this particular case, use the "add-to-load-path" procedure:
> https://www.gnu.org/software/guile/manual/html_node/Load-Paths.html
>
An example usage of "add-to-load-path" is avalilable in Haunt, the
static blog builder
In haunt/ui.scm on line 130 (if my checkout is current)
The first thing the "haunt" command does is it adds the current folder
(getcwd) to the load path so that the rest of the haunt code base can
be found
Hope this helps
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2021-09-19 6:45 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-02 19:49 Can system modify an environment variable in the current environment? Mortimer Cladwell
2021-09-02 20:06 ` Roel Janssen
2021-09-19 6:45 ` adriano
2021-09-02 20:09 ` Taylan Kammer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).