Skip to content

Instantly share code, notes, and snippets.

@camalot
Last active March 8, 2020 03:20
Show Gist options
  • Save camalot/2efbd85fd58753fc1a6aa90c3ff0f5a7 to your computer and use it in GitHub Desktop.
Save camalot/2efbd85fd58753fc1a6aa90c3ff0f5a7 to your computer and use it in GitHub Desktop.
Some helper scripts to allow access of the Windows Environment Variables from within Bash (in WSL)
#!/usr/bin/env bash
source ~/.wsl_helper.bash
eval $(winenv)
#!~/bin/powershell
# Will return all the environment variables in KEY=VALUE format
function Get-EnvironmentVariables {
return (Get-ChildItem ENV: | foreach { "WIN_$(Get-LinuxSafeValue -Value ($_.Name -replace '\(|\)','').ToUpper())=$(Convert-ToWSLPath -Path $_.Value)" })
}
# converts the C:\foo\bar path to the WSL counter part of /mnt/c/foo/bar
function Convert-ToWSLPath {
param (
[Parameter(Mandatory=$true)]
$Path
)
(Get-LinuxSafeValue -Value (($Path -split ';' | foreach {
if ($_ -ne $null -and $_ -ne '' -and $_.Length -gt 0) {
(( (Fix-Path -Path $_) -replace '(^[A-Za-z])\:(.*)', '/mnt/$1$2') -replace '\\','/')
}
} ) -join ':'));
}
function Fix-Path {
param (
[Parameter(Mandatory=$true)]
$Path
)
if ( $Path -match '^[A-Z]\:' ) {
return $Path.Substring(0,1).ToLower()+$Path.Substring(1);
} else {
return $Path
}
}
# Ouputs a string of exports that can be evaluated
function Import-EnvironmentVariables {
return (Get-EnvironmentVariables | foreach { "export $_;" }) | Out-String
}
# Just escapes special characters
function Get-LinuxSafeValue {
param (
[Parameter(Mandatory=$true)]
$Value
)
process {
return $Value -replace "(\s|'|`"|\$|\#|&|!|~|``|\*|\?|\(|\)|\|)",'\$1';
}
}
#!/usr/bin/env bash
# This is the translated path to where the LXSS root directory is
export LXSS_ROOT=/mnt/c/Users/$USER/AppData/Local/lxss
# translate to linux path from windows path
function windir() {
echo "$1" | sed -e 's|^\([a-z]\):\(.*\)|/mnt/\L\1\E\2|' -e 's|\\|/|g'
}
# translate the path back to windows path
function wsldir() {
echo "$1" | sed -e 's|^/mnt/\([a-z]\)/\(.*\)|\U\1\:\\\E\2|' -e 's|/|\\|g'
}
# gets the lxss path from windows
function lxssdir() {
if [ $# -eq 0 ]; then
if echo "$PWD" | grep "^/mnt/[a-zA-Z]/" > /dev/null 2>&1; then
echo "$PWD";
else
echo "$LXSS_ROOT$PWD";
fi
else
echo "$LXSS_ROOT$1";
fi
}
function printwinenv() {
_winenv --get
}
# this will load the output exports of the windows envrionment variables
function winenv() {
_winenv --import
}
function _winenv() {
if [ $# -eq 0 ]; then
CMD_VERB="Get"
else
while test $# -gt 0; do
case "$1" in
-g|--get)
CMD_VERB="Get"
shift
;;
-i|--import)
CMD_VERB="Import"
shift
;;
*)
CMD_VERB="Get"
break
;;
esac
done
fi
CMD_DIR=$(wsldir "$LXSS_ROOT$HOME/\.env.ps1")
echo $(powershell.exe -Command "Import-Module -Name $CMD_DIR; $CMD_VERB-EnvironmentVariables") | sed -e 's|\r|\n|g' -e 's|^[\s\t]*||g';
}
  • .bashrc: Will load all the windows environment variables to your bash environment variables. DO NOT JUST OVERWRITE YOUR .bashrc
  • powershell: A helper script that should be set to execute permission and in a location in your path
  • .wsl_helper.bash: contains some helper functions for loading the environment variables, and converting from/to windows/wsl paths
  • .env.ps1: A powershell script that is executed via WSL->Windows interop to get the environment variables

Put powershell file in a location that is in your path. I use ~/bin and chmod +x powershell. Put .wsl_helper.bash and .env.ps1 in your ~/ path. Edit your ~/.bashrc file and add:

source ~/.wsl_helper.bash
eval $(winenv)
ln -s "$WIN_ONEDRIVE" ~/OneDrive
WIN_ONEDRIVE=/mnt/d/users/rconr/onedrive
PATH=~/bin:/foo:/usr/bin
WIN_PATH=/mnt/c/windows:/mnt/c/windows/system32
#!/usr/bin/env bash
# rename to `powershell`
# chmod +x powershell
. ~/.wsl_helper.bash
PS_WORKING_DIR=$(lxssdir)
if [ -f "$1" ] && "$1" ~= ".ps1$"; then
powershell.exe -NoLogo -ExecutionPolicy ByPass -Command "Set-Location '${PS_WORKING_DIR}'; Invoke-Command -ScriptBlock ([ScriptBlock]::Create((Get-Content $1))) ${*:2}"
elif [ -f "$1" ] && "$1" ~!= "\.ps1$"; then
powershell.exe -NoLogo -ExecutionPolicy ByPass -Command "Set-Location '${PS_WORKING_DIR}'; Invoke-Command -ScriptBlock ([ScriptBlock]::Create((Get-Content $1))) ${*:2}"
else
powershell.exe -NoLogo -ExecutionPolicy ByPass ${*:1}
fi
unset PS_WORKING_DIR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment