Home > Net >  Detect if Powershell or CMD with Rust
Detect if Powershell or CMD with Rust

Time:07-30

I'm writing a CLI app in Rust and need to run commands differently depending on if it is being run in CMD or Powershell. There's this SO question that gives a command "(dir 2>&1 *`|echo CMD);&<# rem #>echo POWERSHELL" to run to output "CMD" or "POWERSHELL", but I can't run that as a Command in Rust.

This code returns the following error: Error { kind: NotFound, message: "program not found" }

let output = Command::new("(dir 2>&1 *`|echo CMD);&<# rem #>echo POWERSHELL")
        .output();

The problem is that some CMD/PowerShell commands don't work from Rust's std::process::Command and need to be called directly with CMD or PowerShell, but without that command, I can't seem to figure out how to detect the shell.

Idk if there's another way with Rust to determine what Windows shell is being ran, but any consistent method would also work for what I have in mind.

Update: To clarify my use case, I'm writing a program to set environment variables temporarily and run a given command provided by the user.

# sets $Env:ENV1=VALUE, then runs the command "dir" with args ["env:"]
temporary-env -e ENV1=VALUE1 dir -- env:

This example doesn't needs to determine the shell type, but other commands like dir have different syntax for args and parameters based on which shell is used, so I want to replicate that behavior based on which shell.

CodePudding user response:

You can execute commands that will only work in one shell (but not crash in the other) to determine which shell you are using. E. g.:

echo %PATH%

Will output the content of your PATH environment variable in CMD, but just the string %PATH% in PowerShell. Vice versa:

echo $env:Path

Will output the content of your PATH environment variable in PowerShell, but just the string $env:Path in CMD. So, depending on the output, you can make a guess on which shell you are using.

This may currently work between CMD and PowerShell, but as phuclv already stated, this is not a stable design for a software. What if the user chooses a third - yet unknown - shell?

CodePudding user response:

Regardless of the platform, whether Windows, Linux, BSD... you never need to determine the current shell type. You decide the shell you want to use and pass a command in that syntax. If you need to run a PowerShell command just run

powershell "temporary-env -e ENV1=VALUE1 dir -- env:"
powershell "$env:MYVAR = "value"; somecommand"

powershell -c dir will always be run as a PowerShell command so the syntax and output will always be consistent. Similarly bash -c dir or zsh -c dir also runs a command in that shell

  • Related