unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Taylan Kammer <taylan.kammer@gmail.com>
To: Mortimer Cladwell <mbcladwell@gmail.com>, guile-user@gnu.org
Subject: Re: Can system modify an environment variable in the current environment?
Date: Thu, 2 Sep 2021 22:09:15 +0200	[thread overview]
Message-ID: <cdfa1826-0111-5dce-1151-22c3e70be55c@gmail.com> (raw)
In-Reply-To: <CAOcxjM7PM9WJN9o523ODeS+s0Ji0vomtXMAh1p2eZxs2jUt8fw@mail.gmail.com>

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



      parent reply	other threads:[~2021-09-02 20:09 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=cdfa1826-0111-5dce-1151-22c3e70be55c@gmail.com \
    --to=taylan.kammer@gmail.com \
    --cc=guile-user@gnu.org \
    --cc=mbcladwell@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).