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.