0

I wish to setup different Windows Terminal launch configurations so that I can use bash on WSL in different windows, each with a different environment profile which is configured from the command line - not by adding a new script each time.

Setting up one WSL is easy, I create a Windows Terminal profile which runs bash with my standard .profile with command:

wsl -d Ubuntu-20.04

However my PC has different Linux JDKs, and I wish have Windows Terminal launchers "JDK18", "JDK19" and "JDK20" each to open WSL bash with different profile per JDK.

Currently I can achieve the above using Windows Terminal profile with the command bash.cmd NN, and bash.cmd is this script:

@echo off
rem Runs bash with no PATH from Windows
set Path=

:: Optional arguments are JDKVER: if x%1 NEQ "" ( set JDKVER=%1 set WSLENV=%WSLENV%::JDKVER ) rem WSLENV is a list of env variables that are passed from Windows to WSL C:\WINDOWS\system32\wsl

Because my .profile contains these lines, the correct JDK NN environment variables are setup in the resulting bash:

[[ -z $JDKVER ]] && export JDKVER=20
export JAVAHOME=/mnt/c/linux/jdk-$JDKVER

Is there a way to define the Windows Terminal launcher which bypasses my use of Windows CMD? Example using something which could configure WSL launch with a different environment setting for each profile in needed in Windows Terminal:

wsl -d Ubuntu-20.04 -envvar JDKVER=xxx
DuncG
  • 552

2 Answers2

2

The comments to this similar question led me to a way to set a WSL/bash environment variable without running CMD as script or scriptlet. This is the Windows Terminal command:

wsl -e bash --noprofile -c "export JDKVER=18 && exec bash --rcfile ~/.profile"

The resulting bash shell picks up the environment variable JDKVER=18 and the profile is loaded incorporating the change. Note that it has side effect of running bash twice.

DuncG
  • 552
1

I think you can achieve this by using additional Windows Terminal profiles.

To call Windows Terminal with a profile use :

WindowsTerminal.exe -p profile-name

The command-line of the profile can be similar to :

cmd.exe /k "set JDKVER=foo&& set WSLENV=%WSLENV%::JDKVER&& wsl -d Ubuntu-20.04"
harrymc
  • 480,290
  • Thanks this works though note that the command-line of the profile mustn't have spaces after foo or the variable JDKVER ends with the space too, change to ...set JDKVER=foo&&... – DuncG Jan 10 '23 at 19:20
  • Changed as suggested. – harrymc Jan 10 '23 at 19:21