$
$ is SSHScript’s command syntax. It runs a command in the current session: locally by default, or on the host selected by $.connect.
SSHScript v3 uses one-dollar syntax for both ordinary commands and shell features. The former $$ syntax remains only for compatibility and is deprecated.
Execute commands and read their result
$hostname
print($.stdout.strip())
$python3 -c "import sys; sys.stderr.write('problem\\n'); sys.exit(7)"
print($.exitcode) # 7
print($.stderr.strip()) # problem
Each command updates these properties:
| Property | Meaning |
|---|---|
$.stdout | Standard output |
$.stderr | Standard error |
$.exitcode | Process exit status |
Capture both output streams directly when useful:
stdout, stderr = $python3 -c "import sys; print('out'); sys.stderr.write('err\\n')"
assert stdout.strip() == "out"
assert stderr.strip() == "err"
The next command replaces $.stdout, $.stderr, and $.exitcode; keep anything needed later in a Python variable.
Command forms
Use the direct form for a literal command. Use $() for a variable command or command options:
command = "python3 -c \\"print('from a variable')\\""
$(command, timeout=5)
name = "SSHScript"
$f'printf "Hello, %s\\n" {name}'
$(["printf", "argv-list"]) # lists and tuples always execute directly
$'printf string-literal'
$r'printf raw-string'
Quote dynamic values before inserting them into a shell command:
import shlex
folder = "/tmp/a folder"
$f'mkdir -p {shlex.quote(folder)}'
Automatic shell detection
V3 inspects string commands with quote awareness. Plain commands execute directly. Shell syntax automatically selects a shell, including pipelines, redirection, logical operators, environment-variable or tilde expansion, globs, assignments, and command substitution.
$printf 'alpha\\nbeta\\n' | grep beta
assert $.stdout.strip() == "beta"
$VALUE=ready; printf '%s\\n' "$VALUE"
$printf 'report\\n' > /tmp/sshscript-report.txt
$printf 'host: %s\\n' "$(hostname)"
Characters inside quotes remain literal, so this is direct execution and prints $HOME rather than expanding it:
$echo '$HOME'
assert $.stdout.strip() == "$HOME"
Override automatic selection when required:
# A list is an unambiguous direct command.
$(["python3", "-c", "import sys; print(sys.argv[1:])", "|", "cat"])
$(command, shell=True) # force the POSIX shell
$('printf bash-shell', shell='bash') # select Bash
$(command, shell=False) # force direct execution
shell_executable='bash' is equivalent to shell='bash'. A list or tuple cannot be used with shell=True.
Migrating from $$
Older documents use $$ for a shell command:
$$ls -l | grep '^d'
Write the v3 form with one dollar:
$ls -l | grep '^d'
Use $ for all new scripts. It chooses direct execution when possible and a shell only when the command needs one.
Local and remote sessions
The syntax is unchanged inside a connection block:
$hostname # local host
with $.connect("ops@example.net"):
$hostname # remote host
See $.connect for authentication and nested connections.