Search code examples
shellunixcommand-lineconemucmder

How can I open shell and then execute a command inside it


What I want is to open default shell, then call another and execute a command there.

Was trying something like this:

c:/Windows/System32/bash.exe -c "zsh & zstyle"

or

cmd /k "c:/Windows/System32/bash.exe -c zsh" & zstyle - this open shell but doesn't run a commands

or

c:/Windows/System32/bash.exe -c "zsh -c 'zstyle'"

Currently I am using a cmder/conemu terminal for windows.


Solution

  • Unfortunately, passing a startup to command to zsh with -c and keeping it open for interactive use (with -i) doesn't work.

    Disclaimer: The following solutions were tested from a regular Command Prompt (cmd.exe), not cmder/conemu, though I'd expect them to work there too.
    To try them from PowerShell (v3+), insert --% as the first argument after (right after bash.exe).

    Here's a workaround:

    c:/Windows/System32/bash.exe -c "zsh -c 'zstyle' && exec zsh -i"
    

    Note that command zstyle is executed in a different, transient zsh instance, so this approach won't work for commands whose purpose is to modify the environment of the interactive shell that stays open.
    If that is a requirement, things get more complicated (this solution courtesy of this answer):

    c:/Windows/System32/bash.exe -c "{ { echo 'zstyle'; echo 'exec 0<&3-';} | zsh -i; } 3<&0"
    

    Note, however, that both commands being executed will be printed before their output, if any, is shown, preceded by the prompt - as if the commands had been typed interactively.