sh
The sh module provides functions for executing POSIX Shell, Bash, and mksh commands. The implementation uses the mvdan.cc/sh interpreter instead of relying on the system shell, and therefore provides a consistent experience across all platforms (including Windows).
- sh.exec(command: str, cwd: str = None, env: dict = None, try_: bool = None) str | None
Execute a shell command. The command must be a valid POSIX Shell, Bash, or mksh command. Any commands that are not shell builtins must be available on the path. If the command fails, the calling module will abort unless try_ is set to True, in which case the contents of standard error will be returned.
- Parameters:
command – the command to execute.
cwd – the working directory for the command. Defaults to the calling module’s directory.
env – any environment variables to set when running the command.
try_ – when True, the calling module will not be aborted if the shell command fails.
- Returns:
the contents of standard error if try_ is set and None otherwise. To capture the command’s output, use output.
- sh.output(command: str, cwd: str = None, env: dict = None, try_: bool = None) str | tuple
Execute a shell command and return its output. The command must be a valid POSIX Shell, Bash, or mksh command. Any commands that are not shell builtins must be available on the path. If the command fails, the calling module will abort unless try_ is set to True, in which case the contents of standard error will be returned.
- Parameters:
command – the command to execute.
cwd – the working directory for the command. Defaults to the calling module’s directory.
env – any environment variables to set when running the command.
try_ – when True, the calling module will not be aborted if the shell command fails.
- Returns:
the contents of standard output if try_ is not truthy and the command succeeds. If try_ is truthy, output returns (stdout, True) if the command succeeds and (stderr, False) if the command fails.