1

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?

  • 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 8: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
1

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.
  • 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
  • 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
    Check Using command-line arguments for Windows Terminal and this thread as well. – JosefZ Aug 18 '20 at 16:26
  • 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 9:58
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.