get_shell_command

Function get_shell_command 

Source
pub const fn get_shell_command() -> (&'static str, &'static str)
Expand description

Returns the appropriate shell command and flag for the current platform.

This function returns the platform-specific shell executable and the flag used to execute a command string. This is used for running shell commands in a cross-platform manner.

§Returns

A tuple of (shell_command, execute_flag):

  • Windows: ("cmd", "/C")
  • Unix-like: ("sh", "-c")

§Examples

use agpm_cli::utils::platform::get_shell_command;
use std::process::Command;

let (shell, flag) = get_shell_command();

let output = Command::new(shell)
    .arg(flag)
    .arg("echo Hello World")
    .output()?;

println!("Output: {}", String::from_utf8_lossy(&output.stdout));

§Platform Commands

  • Windows: Uses cmd.exe with /C flag to execute and terminate
  • Unix-like: Uses sh with -c flag for POSIX shell compatibility

§Use Cases

  • Executing shell commands in a cross-platform way
  • Running system utilities and tools
  • Batch operations that require shell features
  • Environment-specific command execution

§Security Considerations

When using this function with user input, ensure proper escaping and validation to prevent command injection vulnerabilities.

§Alternative Shells

This function returns the most compatible shell for each platform. For specific shell requirements (bash, PowerShell, etc.), use direct command execution instead.

§See Also