wsl pass env values to a windows process [duplicate] - environment-variables

From a Windows Subsystem for Linux (v1) Alpine bash terminal, I would like to set an environment variable that get's passed into a windows executable. Is there any way to do this?
example of what I was hoping would print "Hello, World!":
windows-10:~# export X=World
windows-10:~# cmd.exe /c 'echo Hello, %X%!'
Hello, %X%!
See answer from Philipe below.
Here is a copy of the pertinent info from https://learn.microsoft.com/en-us/windows/wsl/interop
Share environment variables between Windows and WSL
Available in Windows Insider builds 17063 and later.
Prior to 17063, only Windows environment variable that WSL could access was PATH (so you could launch Win32 executables from under WSL).
Starting in 17063, WSL and Windows share WSLENV, a special environment variable created to bridge Windows and Linux distros running on WSL.
Properties of WSLENV:
It is shared; it exists in both Windows and WSL environments.
It is a list of environment variables to share between Windows and WSL.
It can format environment variables to work well in Windows and WSL.
There are four flags available in WSLENV to influence how that environment variable is translated.
WSLENV flags:
/p - translates the path between WSL/Linux style paths and Win32 paths.
/l - indicates the environment variable is a list of paths.
/u - indicates that this environment variable should only be included when running WSL from Win32.
/w - indicates that this environment variable should only be included when running Win32 from WSL.
Flags can be combined as needed.

Can you try this ?
~$ export X=World
~$ export WSLENV=X/w
~$ cmd.exe /c 'echo Hello, %X%!'
Hello, World!

Related

How do I read Linux Environment Variables within Wine?

I have some native Linux apps and some Wine apps that need to store configuration. I want to write the config data to ~/.config/some/path. But I'm not sure how to get to that directory from Wine.
I tried reading the Linux environment variables, but it doesn't work:
user#ubuntu:~$ echo $HOME
/home/user
user#ubuntu:~$ wine cmd
Microsoft Windows 10.0.17134
Z:\home\user\.wine>echo %HOME%
%HOME%
I also tried using ~, but that just creates a directory named ~ in the current working directory.
Any advice? How can I access the ~/.config directory from within a wine app?

The "code" command does not work when connecting to a Docker container remotely with VSCode

I am using VSCode in my local PC and connecting to a Docker container in a remote server with VSCode's Extensions of Remote - SSH and Remote - containers. However, when I type
$ code <file name>
on the VSCode's terminal (Bash), I get an error messages saying that
bash: code: command not found
and I can't edit the file on the VSCode's editor.
If I click on the file from VSCode's Explorer (Ctrl+Shift+E), the edit screen will appear, but isn't it possible to call it with the code command?
Also, I call the command palette (Ctrl+Shift+P) and then search for Shell Command: Install 'code' command in PATH but no matching commands are found.
The execution environment is as follows:.
Local PC: Windows 10 Pro
Remote host PC: Ubuntu 18.04.3 LTS
Docker container in the remote host PC: Ubuntu 18.04.3 LTS
Thank you very much for your answer.
I'll answer myself as I was able to call VSCode using the code command in the remote's container.
When I look closely underneath home, I see that there was a code at the following directory.
$HOME/.vscode-server/bin/<directory with a hash-like name>/bin/
So I passed the PATH through it and it worked.
By the way, <directory with a hash-like name> is a directory with a hash-like name, which is randomly assigned when you connect to the container remotely. It's different every time, so please refer to it by yourself.
The way to register the path is as follows.
export PATH="$PATH:$HOME/.vscode-server/bin/<directory with a hash-like name>/bin/"
Thank you very much for your support.
They posted an easy solution in response to the issue created by #davetapley here.
Go to the VSCode settings, search for "terminal integrated inherit env" and enable the option. The code command will be available the next time you open a terminal.
(This should have been a comment but I don't have that privilege yet!)
Here is what worked for a similar problem where the code command was not working as expected on my Linux system, connected to my windows PC via VSCode's Extensions of Remote - SSH: adding VS Code to PATH by editing the ~/.bashrc file in my linux remote system.
I used the path variable from akki's answer, and the procedure detailed in this answer by oadams.
To edit this file in my system, I use nano:
nano ~/.bashrc
at the end the file, add the export path statement akki mentioned, just without the quotation marks:
export PATH=$PATH:$HOME/.vscode-server/bin/<directory with a hash-like name>/bin/
However, my hashtag-like-name of the code mentioned in akki's answer does not change when I remote SSH to my Raspberry Pi, so I am not sure how to fix that part of the problem.
My execution environment is as follows:
Local PC: Windows 10 on Dell Latitude PC.
Remote host PC: Raspbian GNU/Linux 10 (buster) on Raspberry Pi 3B.
It sounds like you are confusing which place you are writing the code command. Your installation of Visual Studio Code is local on your machine and not inside the docker container. When you open a terminal inside the docker container this is as if it was a different machine altogether. Here is a link to vscode documentation that is both interesting and useful.
Derived from #akki's answer, I noticed that the hash is stored in several environment variables. So I added this to my .zshrc which simply finds the path to the bin and then makes an alias.
VSCODE_SSH_BIN=$(echo "$BROWSER" | sed -e 's/\/helpers\/browser.sh//g')
alias code='$VSCODE_SSH_BIN/remote-cli/code'

Erlang not working with internal Terminal in Visual Studio Code

I am currently having an issue with visual studio code and erlang, I have erlang/OTP installed on my PC, and I have installed the erlang plugin for visual studio code, however I cannot run erlang from the internal terminal window. When I try to I receive this error:
erl : The term 'erl' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the
path is correct and try again.
At line:1 char:1
+ erl
+ ~~~
+ CategoryInfo : ObjectNotFound: (erl:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
any help would be appreciated.
From the VS docs
The shell [that is] used [by VS] defaults to $SHELL on Linux and OS X, and %COMSPEC% on
Windows. These can be overridden manually by setting
terminal.integrated.shell.* in settings.
So I would check what the value is of your %COMSPEC% environment variable. Or, you can override %COMSPEC% in the VS settings:
From the VS docs:
Correctly configuring your shell on Windows is a matter of locating
the right executable. VS Code defaults to the %COMSPEC% environment
variable on Windows which typically points to the 32-bit version of
cmd.exe.
Below are a list of common shell executables and their default
locations:
// 64-bit cmd if available, otherwise 32-bit
"terminal.integrated.shell.windows": "C:\\Windows\\sysnative\\cmd.exe"
// 64-bit PowerShell if available, otherwise 32-bit
"terminal.integrated.shell.windows": "C:\\Windows\\sysnative\\WindowsPowerShell\\v1.0\\powershell.exe"
// Git Bash
"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe"
// Bash on Ubuntu (on Windows)
"terminal.integrated.shell.windows": "C:\\Windows\\sysnative\\bash.exe"
You may also want to check if the path to erlang is in your %PATH% environment variable, and if not add it. Have you ever successfully started up the erlang shell before, e.g. after you installed erlang to test that it was working?
Try the following guide and install erlang
https://www.rose-hulman.edu/Users/faculty/young/CS-Classes/resources/Erlang/index.html

MinGW changes the HOME environment variable

I'm using MinGW and I'm trying to set an environment variable called HOME.
I set the variable to "C:\Users\jdoe" but when I run printenv from the command prompt it tells me the value of HOME is "/c/Users/jdoe".
I've test out other variables names, and this change doesn't happen. Only for HOME. When I remove the HOME environment variable printenv no longer lists HOME.
Some simple programs like mkdir and rm don't mind this change but gcc is having none of it. I'd like to be able to build this project on both windows and linux. Having a working HOME variable would be very useful. Why is this happening and where can I make it stop?
By "run printenv from the command prompt" you mean running it from the MSYS prompt?
If this is the case, the change of the HOME environment variable can be justified because MSYS tries to give you a GNU/Linux-like environment, where there aren't drive letters. There is only one filesystem, the /, and everything else is mounted into this.
Hence, in MSYS C:\ becomes /c/, etc...
The /c/Users/jdoe in MSYS is the same directory as C:\Users\jdoe in cmd.exe, or in the explorer.

How to run Ruby and GIT commands in one place on Windows

I have Ruby and GIT installed on my windows box. To run GIT commands I am utilizing the GIT Bash. To run Ruby commands I am using the command line.
I have not been successful running GIT commands from the CMD line nor can I seem to run Ruby commands from inside the GIT Bash. I would love to be able to run commands for both GIT and Ruby from the GIT Bash (ideal) or at the least from the CMD line.
What is the best way to go about this?
I run git commands from the CMD session all the time.
Make sure your PATH environment variable includes the 'cmd' directory from a msysgit distro:
Path=C:\Path\To\Git\1.7.1\cmd
If not, add it in your session:
set PATH=%PATH%;C:\Path\To\Git\1.7.1\cmd
and you are done. Git and Ruby commands in your CMD shell.
The reverse (Ruby commands) is possible in a Git bash, by adding to the PATH a value like /c/path/to/Ruby/186-27/bin
To elaborate on VonC's answer of making Ruby available in Git-Bash.
All you have to do is add the path to your Ruby bin folder in your windows environment variables. It doesn't have to be in the format /c/path/to/ruby, it can be C:\Ruby193\bin.
Step by step for Windows 7:
Start
Search programs and files (default textbox after hitting the Start icon), Search for 'environment'
Select 'edit the system environment variables'
Click 'Environment Variables' (bottom right of the form)
Add to the 'System Variables' 'PATH' the following ';C:\Ruby193\bin' (without the single quotes)
Restart your shell
Make sure to close your git-bash shell and restart it to pick up the new environment variable.
Go to My Computer -> Properties -> Advanced system settings ->
Environment Variables
Add a New System variable. Variable name = RUBY_BIN. Variable
value = C:\Ruby193\bin (path may vary).
Add a New System variable. Variable name = MSYSGIT_BIN. Variable
value = C:\msysgit\bin (path may vary).
Append ;%RUBY_BIN%;%MSYSGIT_BIN% to Path variable, under System variables.
Restart shell.
This will allow you to run ruby, git or sh (Git Bash) commands from Command Prompt, as well as Ruby from Git Bash.
My personal setup uses msysgit and tortoisegit. I've found that using TortoiseGit's Pageant manager for the keys lets me use everything from any command line, including powershell. The only annoyance is I have to have pageant running with the keys added, which seem to clear on every reboot. Fortunately I don't reboot often.
The combined answer for VonC and Rots helped me to achieve the desired results.
However, since I was not familiar with editing environment variables, I must have accidentally overwritten the path for my nodejs files.
As a result my solution included adding the ruby path and the nodejs path to my user variables instead of the system.
I'm using a Windows 7 machine.
while installing ruby installer, select the option "Add ruby executables to your path" . Then all git/ruby commands will run from git bash.

Resources