9

I would like to start Ubuntu WSL [shell: bash] inside Windows Terminal from a batch file and pass a command, which should immediately run after startup.

  • When directly starting WSL, I'm able to do this using the -c argument; e.g. the following opens WSL and establishes an SSH connection with some local port forwardings:
    bash -c "ssh -L 3306:localhost:3306 -L 5900:localhost:5900 -L 8001:localhost:8001 user@target.com"
    

How would I achieve this using Windows Terminal?

JW0914
  • 7,865
suamikim
  • 337
  • 1
    Which shell are you using in Windows Terminal and what error are you getting when running the above command? – harrymc Aug 18 '20 at 08:36
  • @suamikim - You should edit your question instead of replying with a temporary comment – Ramhound Aug 18 '20 at 12:20
  • Windows Terminal is still in a beta stage and it's not possible to do what you want because it still lacks broad functionality; however, ConEmu can. Another option is to use Task Scheduler since WSL's terminal has been hardcoded into Windows [it's available via the context menu, so it can likely be launched via Powershell]. – JW0914 Aug 18 '20 at 13:49
  • @JW0914 Could you give a little more detail about how I could use the Task Scheduler to go about this? – suamikim Aug 18 '20 at 13:53
  • @suamikim I can't provide a complete answer since I'm not proficient in Powershell, however it should be possible to launch a WSL terminal via a Powershell script, either having the command issued from the Powershell script or in a separate Bash script in WSL. In Task Scheduler, you'd configure the script to run at either Startup or Login. Out of curiosity, have you considered executing from WSL's /etc/rc.local script, crontab, etc., of which bypasses the Windows side of it altogether? – JW0914 Aug 18 '20 at 14:02
  • @JW0914 Launching wt from batch/PowerShell/... is not the issue here but handing it a cmd to execute after startup is. Also I don't want to run my batch file on a scheduled basis but execute it manually, so I don't think the Task Scheduler will help me in any way. Using rc.local or crontab also don't really help me here since I don't want to run my cmd EVERYTIME I start the WSL bash but only when started from my batch file... Thanks for your input! – suamikim Aug 18 '20 at 14:22
  • @suamikim I wasn't talking about launching wt from a script, as wt lacks basic terminal emulation functionality (it's still a beta). What I was talking about is issuing the WSL Bash terminal command directly via a Powershell script and is likely the only way to natively do what you want to do. As I previously mentioned, you can do exactly what you're trying to do via ConEmu by adding a Task [a terminal profile] in Settings → Startup → Tasks (you can also specify variables in Settings → Startup → Environment). ConEmu should auto-add the WSL terminal during install/first launch. – JW0914 Aug 18 '20 at 14:42
  • @JW0914 Ok, then I still don't get what you really have in mind. What exactly do you mean by "issuing the WSL Bash terminal command directly via a Powershell script"? Using PowerShell, I'd still have to know how to pass the correct params to the wt start cmd? So I don't really see how PowerShell gives me more "power" here then a regular bash script? Regarding ConEmu: Thanks for the tip, however I'm not looking for an alternative to wt ATM. – suamikim Aug 18 '20 at 14:50
  • @suamikim wt is a Terminal Emulator, not a terminal... the terminal is Bash. To make this simpler, simply install ConEmu and you can issue the command as written in your question. (Since WSL'S Bash terminal is now hardcoded into Windows 10 [it's available in the Context Menu], it should be able to be called from a Powershell script, but to keep this simple, simply use ConEmu instead of Windows Terminal, at the very least until it's finalized software with the full functionality of a terminal emulator.) – JW0914 Aug 18 '20 at 14:55

2 Answers2

8

I found two ways, with both working so far:

  1. Create a dedicated profile:
    The commandline option also accepts arguments, which are directly passed to the shell on startup, allowing the direct usage of wsl.exe with an additional "startup command":
    {
      "guid": "{...}",
      "hidden": false,
      "name": "Ubuntu SSH",
      "commandline": "wsl.exe ssh -L 3306:localhost:3306 -L 5900:localhost:5900 -L 8001:localhost:8001 user@target.com",
    }
    
    I can now start the shell via a batch file, which runs using the below; however, the downside is the new profile is shown in Windows Terminal's Profile Selection Menu:
    wt -p "Ubuntu SSH"
    

  2. Pass commandline to wt:
    As shown in Option 1, the commandline option can include further parameters for the target shell, with the following working when called from a batch file:
    wt wsl.exe ssh -L 3306:localhost:3306 -L 5900:localhost:5900 -L 8001:localhost:8001 user@target.com
    
    I did not find any official online resources explaining why this works, adjusting the suggestions in this answer, so I'm not sure whether this is supposed to work this way or if it's a hidden/unintentional feature that could break in future versions.
suamikim
  • 337
  • Regarding the last paragraph, terminal emulators are supposed to be able to function like that, wt simply lacks the full functionality of basic terminal emulation because it's still beta software (if VS Code is any indication, once wt becomes RTM as finalized software, it will likely be just as configurable and functional as ConEmu, one of the most feature-rich and customizable terminal emulators for Windows) – JW0914 Aug 18 '20 at 15:25
  • 1
    @JW0914 Thanks for the additional info! However, could you please stop promoting ConEmu so hard? Whilst I'm pretty sure it's very good software, it has very little to do with my initial question & mentioning it over & over again is therefore simply besides the topic here... – suamikim Aug 18 '20 at 15:36
  • It's not about promoting ConEmu, it's the fact ConEmu works where Windows Terminal does not because, as I've repeatedly stated, Windows Terminal is Beta software, lacking even the most basic terminal emulation functionality. You asked how to execute your command and ConEmu does so w/o any workaround simply by calling the default task [Bash] that is auto-added during install to run the command within that terminal (Bash task output: set "PATH=%ConEmuBaseDirShort%\wsl;%PATH%" & %ConEmuBaseDirShort%\conemu-cyg-64.exe --wsl -cur_console:pm:/mnt). Either way, I'm done talking in circles. – JW0914 Aug 18 '20 at 15:46
  • I understood your argument about ConEmu & WT being beta right from the beginning and appreciate you showing up the alternative. However, my question was always "how to execute the command using WT"... – suamikim Aug 18 '20 at 15:47
  • 2
    wt without -p flag runs default profile (which is Windows Powershell initially)… Also note that Microsoft retired Bash on Ubuntu on Windows once they introduced multiple WSL distributions. Run wsl.exe [command] instead of bash.exe [-c "command"] . – JosefZ Aug 18 '20 at 16:17
  • 2
  • 1
    @JosefZ Using wsl.exe "command" instead of bash.exe -c "command" gives me this error: /bin/bash: ssh -L 3306:localhost:3306 -L 5900:localhost:5900 -L 8001:localhost:8001 user@target.com: command not found – suamikim Aug 19 '20 at 05:57
  • 1
    Oh yes, I know… That's why I gave both examples bash.exe [-c "command"] versus wsl.exe [command] (note the presence/absence of double quotes). – JosefZ Aug 19 '20 at 09:58
  • @JosefZ Thanks for the additional info! Without the quotes, the command runs as described! – suamikim Aug 19 '20 at 11:08
  • Do you know why this command gives error? wt -p "Ubuntu18" phpstorm.sh I just want to start a script that I have globally once WSL is opened, it opens but does not execute phpstorm.sh – jcarlosweb Mar 03 '21 at 14:02
0

The error message is correct : Windows Terminal does not have a -c parameter.

The syntax is:

wt [options] [command ; ]

Where the only options are:

  -h,--help                   Print this help message and exit
  -v,--version                Display the application version
  -M,--maximized Excludes: --fullscreen
                              Launch the window maximized
  -F,--fullscreen Excludes: --maximized
                              Launch the window in fullscreen mode
  -p                          Profile
  -d                          Directory
harrymc
  • 480,290