[SERVER-4895] Expose environment variables in the shell Created: 07/Feb/12  Updated: 06/Dec/22  Resolved: 19/Nov/21

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Scott Hernandez (Inactive) Assignee: Backlog - Server Tooling and Methods (STM) (Inactive)
Resolution: Won't Fix Votes: 12
Labels: move-stm, neweng, platform-lhf
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
is duplicated by SERVER-10570 Reading environment variables Closed
Related
is related to SERVER-18877 Allow MongoRunner.runMongod Take In E... Closed
Assigned Teams:
Server Tooling & Methods
Participants:

 Description   

Something like

var user = Env.get("USERNAME");
//and less useful, possibly
Env.set("MyVar", 12)
run(...)



 Comments   
Comment by Orgad Shaneh [ 21/Apr/21 ]

_getEnv function was added inĀ SERVER-47355. Is it intentionally not documented?

Comment by Matt Kalan [ 24/Feb/17 ]

This came up in a Docker setup this week (on Windows) which I think might make this requirement increasingly more common, as often configuration will be scripted and parameters passed in dynamically

Comment by Kevin Pulo [ 02/Mar/16 ]

$HOME is another variable that can be very useful to know the value of.

The functions could possibly also be called getenv()/setenv() (but otherwise work roughly the same), since those are the underlying POSIX libc functions that the implementation would use.

Also unsetenv() or Env.unset() to unset environment variables. And maybe clearenv() (which is not in POSIX).

A more convenient interface might be a magic object whose fields represent the environment values. I'm pretty sure that a Proxy (now that we have ES6) could be used to implement this cleanly by hooking field get/set/delete and doing the necessary thing, ie. this could be a pure js convenience layer on top of lower-level (natively implemented) getenv()/setenv()/unsetenv().

Comment by James M. Greene [ 10/Jul/15 ]

I followed a similar pattern as @Justin's comment except that I passed in the environment variables by using an

--eval

option string (plus the

--shell

option, if you want it to stay open) rather than relying on temporary files and a "~/.mongorc.js" file.

It's a pretty solid workaround but I'd definitely rather that I didn't need to do so at all.

Comment by Justin [ 18/Mar/13 ]

A work around I use:

alias mongo="export | sed 's/declare -x /env./' > shell.js; mongo $@; rm shell.js;"

In ~/mongorc.js:

var env = {};
load('shell.js');

You can change shell.js to something more unique or add it to /tmp so you don't clobber something inadvertently.

Generated at Thu Feb 08 03:07:17 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.