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 [email protected]"
    

How would I achieve this using Windows Terminal?

9
  • 1
    Which shell are you using in Windows Terminal and what error are you getting when running the above command?
    – harrymc
    Commented Aug 18, 2020 at 8:36
  • @suamikim - You should edit your question instead of replying with a temporary comment
    – Ramhound
    Commented Aug 18, 2020 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
    Commented Aug 18, 2020 at 13:49
  • @JW0914 Could you give a little more detail about how I could use the Task Scheduler to go about this?
    – suamikim
    Commented Aug 18, 2020 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
    Commented Aug 18, 2020 at 14:02

2 Answers 2

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 [email protected]",
    }
    
    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 [email protected]
    
    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.
10
  • 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
    Commented Aug 18, 2020 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
    Commented Aug 18, 2020 at 16:17
  • 2
    Check Using command-line arguments for Windows Terminal and this thread as well.
    – JosefZ
    Commented Aug 18, 2020 at 16:26
  • 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 [email protected]: command not found
    – suamikim
    Commented Aug 19, 2020 at 5: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
    Commented Aug 19, 2020 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
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .