Command
Awaitable shell command.
By default, awaiting a command returns the exit-code. Use one of the methods such
as .output()
to modify the return value.
Examples:
>>> returncode = await sh(t"echo hello world")
hello world
>>> returncode
0
Construct a command.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Template
|
t-string template for command. |
required |
|
bool | None
|
Suppress stdout and stderr from displayed in the terminal. |
None
|
|
bool | None
|
Raise exception when command return code is non-zero. |
None
|
|
str | bytes | None
|
Pass standard input to command. |
None
|
|
str | Path | None
|
Shell's working directory. |
None
|
|
dict[str, str] | None
|
Dictionary of environment variables. |
None
|
check: bool = True
class-attribute
instance-attribute
Raise exception when command return code is non-zero. Globally affects all command invocations.
cwd: str | Path | None = None
class-attribute
instance-attribute
Shell's working directory. (Defaults to current working directory) Globally affects all command invocations.
quiet: bool = False
class-attribute
instance-attribute
Suppress stdout and stderr from displayed in the terminal. Globally affects all command invocations.
bytes() -> Command[str]
Stdout returns as bytes.
Examples:
>>> await sh(t"echo hello").bytes()
b'hello\n'
json() -> Command[Any]
Stdout returns as JSON.
Examples:
>>> await sh(t"echo {json.dumps({"hello": "world"})}").json()
{'hello': 'world'}
output() -> Command[CompletedCommand]
Capture stdout and stderr as bytes.
Examples:
>>> await sh(t"echo hello").output()
CompletedCommand(returncode=0, stdout=b'hello\n', stderr=b'')
text() -> Command[str]
Stdout returns as string.
Examples:
>>> await sh(t"echo hello").text()
hello
toml() -> Command[Any]
Stdout returns as TOML.
yaml() -> Command[Any]
Stdout returns as YAML.