Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify Environment Variables (for example $env:PSModulePath) on Linux #4341

Closed
rudolfvesely opened this issue Jul 25, 2017 · 7 comments
Closed
Labels
Issue-Discussion the issue may not have a clear classification yet. The issue may generate an RFC or may be reclassif Issue-Enhancement the issue is more of a feature request than a bug Resolution-No Activity Issue has had no activity for 6 months or more WG-Engine-Providers built-in PowerShell providers such as FileSystem, Certificates, Registry, etc.

Comments

@rudolfvesely
Copy link

Hi all,

I think it would be great if you can change Environment Variables (accessed for example by $env:Name or Get-ChildItem env:) on Linux.

For example to set there values a new configuration file in /etc ?

On Windows I can change for example PSModulePath and add a custom path. The only option on Linux is to create a profile ps1 file for all users.

Thank you.

@mklement0
Copy link
Contributor

mklement0 commented Jul 25, 2017

Can I suggest that you change the title of your issue to reflect the fact that you're looking for PowerShell to introduce a profile-independent way to persistently define environment variables?

A few thoughts:

  • Environment variables are by definition shell/application-agnostic and are seen by all descendant processes, irrespective of what shell/application created them.

  • PowerShell "listens to" only 2 environment variables, $env:PSModulePath being one of them.

  • From what I understand, Linux environments (all of them?) already have file /etc/environment as a system-wide, shell-independent mechanism for defining environment variables.

  • That said, if a set of environment variables is truly only used by PowerShell, then the system-wide PowerShell profile - $profile.AllUsersAllHosts - arguably is the right place to define them.

    • This solution would also work for macOS, which doesn't have a system-wide mechanism for defining environment variables (anymore).
    • (macOS has /etc/profile, which the default shell is expected to process, which (a) doesn't take effect for GUI applications, and (b) requires the default shell to honor this expectation and (c) requires the default shell to understand POSIX-shell syntax (which PowerShell, if it were defined as the default shell, wouldn't [Update: /etc/profile and ~/.profile now are honored if you start with pwsh -l or use PowerShell as the default shell]).

@iSazonov iSazonov added the Issue-Discussion the issue may not have a clear classification yet. The issue may generate an RFC or may be reclassif label Jul 27, 2017
@danwize
Copy link

danwize commented Dec 24, 2019

I tried setting an environment variable like this on an Ubuntu machine:
[System.Environment]::SetEnvironmentVariable('ASPNETCORE_ENVIRONMENT', 'HypervDev', [System.EnvironmentVariableTarget]::Machine)

I was hoping this would edit the /etc/environment file to set the environment variable system wide.

@vexx32
Copy link
Collaborator

vexx32 commented Dec 24, 2019

Unfortunately .NET Core have yet to implement a method to set environment variables in Unix systems. The documentation for those methods states that applying values to anything other than the current process is only supported on windows. ☹️

@vexx32 vexx32 added WG-Engine-Providers built-in PowerShell providers such as FileSystem, Certificates, Registry, etc. Issue-Enhancement the issue is more of a feature request than a bug labels Dec 24, 2019
@sumit84mca
Copy link

I tried setting an environment variable like this on an Ubuntu machine: [System.Environment]::SetEnvironmentVariable('ASPNETCORE_ENVIRONMENT', 'HypervDev', [System.EnvironmentVariableTarget]::Machine)

I was hoping this would edit the /etc/environment file to set the environment variable system wide.

Is there any update on this or any work around

@vexx32
Copy link
Collaborator

vexx32 commented Jun 8, 2022

No, you'd need to file an issue in the https://github.com/dotnet/runtime repository to request the functionality be expanded for non-Windows operating systems.

A workaround for now would be to modify the /etc/environment file directly instead.

GitHub
.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps. - GitHub - dotnet/runtime: .NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.

@rbleattler
Copy link

I've written a function which can be used to accomplish this when running elevated (as su) as part of my xEnvironmentVariables Module. I don't know how helpful it is to anyone, but thought I'd share Set-NixEnvironmentVariable.ps1. Hope this is helpful to anyone landing here looking for a solution.

@microsoft-github-policy-service microsoft-github-policy-service bot added the Resolution-No Activity Issue has had no activity for 6 months or more label Nov 16, 2023
@mieliespoor
Copy link

Can we re-open this as this still seem to be an issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Discussion the issue may not have a clear classification yet. The issue may generate an RFC or may be reclassif Issue-Enhancement the issue is more of a feature request than a bug Resolution-No Activity Issue has had no activity for 6 months or more WG-Engine-Providers built-in PowerShell providers such as FileSystem, Certificates, Registry, etc.
Projects
None yet
Development

No branches or pull requests

8 participants