A command can work locally and still behave differently when you run it through SSH. The reason is often not SSH itself: there are two shell-parsing steps to consider. Your local shell parses the command first; after connecting, a remote shell parses the command string it receives.
That distinction matters for variables, quotes, wildcard characters, command substitutions, and other syntax. The fix for a short command is usually to quote it so the local shell leaves the remote expression alone. For a longer command, a quoted heredoc can send a script to the remote shell without expanding its contents locally.
The command may be parsed twice
When you run:
ssh app 'some command'
your local shell processes the line and starts ssh. SSH sends a command string to the server, where the account’s configured shell interprets it. The exact remote shell depends on the account and server setup; it is not necessarily Bash.
Local quoting controls what reaches SSH. It does not, by itself, stop the remote shell from interpreting the command. In particular, single quotes around the command are syntax for your local shell: they keep local expansions out, but those quote characters are not a protective wrapper around the command on the remote machine.
For example:
ssh app 'printf "remote HOME=%s\n" "$HOME"'
The local shell removes the outer single quotes and passes the command text to SSH. The remote shell then interprets " as quoting for the printf argument and expands $HOME. The value printed is the remote account’s home directory, not the local one.
Compare that with a locally expanded variable:
ssh app "printf 'home=%s\\n' \"$HOME\""
Here the local shell sees double quotes around the command, so it expands $HOME before SSH gets the command. The escaping is also harder to read. If you want the remote shell to expand a variable, single-quote the command at the local level and use double quotes around the variable within the remote command.
Watch for more than variables
The same rule applies to command substitutions and globs. In this example, the single quotes keep the wildcard from expanding against local files:
ssh app 'printf "%s\n" /var/log/*.log'
The remote shell receives /var/log/*.log and expands it against the remote filesystem. If there are no matches, what happens depends on the remote shell’s settings; some shells leave the pattern unchanged, while others can treat an unmatched glob as an error.
Likewise, this command substitution runs remotely:
ssh app 'printf "remote host=%s\n" "$(hostname)"'
A common mistake is to use double quotes around the whole command and assume the remote shell will handle everything. The local shell may perform $(...) or $variable expansion first. If the local value is empty, SSH cannot restore the intended expression afterward.
Send a script with a quoted heredoc
As a command grows, nested quote escaping gets fragile. A quoted heredoc delimiter tells the local shell not to expand variables, command substitutions, or backslash escapes in the heredoc body:
ssh app 'bash -s' <<'REMOTE'
printf 'remote host=%s\n' "$(hostname)"
printf 'remote home=%s\n' "$HOME"
REMOTE
The local shell passes the heredoc body as standard input to ssh. SSH runs bash -s remotely, and bash -s reads the script from standard input. The quoted delimiter, 'REMOTE', is the important part: without those quotes, the local shell could expand $HOME or $(hostname) before the script is sent.
The delimiter name is arbitrary, but it must appear by itself at the end of the heredoc. If the body contains that exact delimiter on a line by itself, choose another name. A heredoc sent this way does not allocate a terminal, which is appropriate for ordinary scripts. For commands that need an interactive terminal, SSH’s -t option may be necessary, but it changes how input and output are handled.
When local values are intentional
Sometimes a script needs a local value as an input. Rather than accidentally expanding a whole script, pass the value explicitly as an argument or environment variable. For example, this passes a local shell variable as a positional argument to the remote Bash script:
local_file='/tmp/report.txt'
ssh app 'bash -s -- "$1"' _ "$local_file" <<'REMOTE'
remote_file=$1
printf 'requested path: %s\n' "$remote_file"
REMOTE
The _ becomes the remote Bash process’s $0; the following argument becomes $1. The local shell expands "$local_file" as an argument to ssh, preserving spaces in its value. The script body remains protected by the quoted heredoc delimiter. Do not treat this as a universal way to safely pass arbitrary data: shell arguments have size limits, and data should be handled with care if it might contain sensitive values or be interpreted as code.
For larger scripts, another clear option is to keep the script in a local file and send it to Bash over SSH:
ssh app 'bash -s' < ./check-service.sh
The file’s contents are read by the local shell’s input redirection, but they are not interpolated as part of the command line. Bash interprets them on the remote side.
A quick debugging checklist
When a remote command produces an unexpected value, ask:
- Which shell expands this
$variableor$(command)first? - Are the outer quotes intended to protect text from the local shell, or are they meant for the remote shell?
- Should a wildcard match local files or remote files?
- Would a quoted heredoc make a multi-line command easier to audit?
You can inspect the SSH configuration selected for an alias with ssh -G app; this helps confirm which host and options the client will use, though it does not show how the remote shell will parse your command. For examples of aliases and other client-side settings, see the practical SSH config examples. If you are issuing the command from Windows, remember that your local shell’s quoting rules may differ; the PowerShell OpenSSH workflow guide covers connecting and working from PowerShell. And if the issue is choosing or configuring the Windows SSH client rather than parsing a command, see the Windows SSH client comparison.
The reliable habit is to decide deliberately which side should expand each piece of syntax. Quote the command to protect remote expressions from local expansion; use a quoted heredoc when the command is really a script.
I'm building ShadeTerm — an SSH client that organizes terminals, SFTP, code editing, and server tools into workspaces.
Top comments (0)