Escape a command for every shell between you and the far end.
Uses of Nested shell quoting
- Running a one-liner on a server over ssh from a local terminal
- Wrapping a command in sudo sh -c inside an Ansible or CI task
- Sending a command with spaces and quotes into docker exec
- Building a remote command inside a shell script without testing it against production
Nested shell quoting pitfalls
- Adding backslashes until the error stops, without ever seeing what the far end received
- Quoting for one shell when the command passes through two or three
- Expecting a variable or a glob to resolve locally when the quoting protects it for the far side
- Mixing single and double quotes so the outer layer ends early and the rest becomes separate arguments
Nested shell quoting questions (6)
How many levels of quoting does my command need?
One level for every shell that parses the command before the last one runs it. Sending a command over ssh means the local shell reads the line and the remote shell reads what arrives, so the command needs one level. Adding sudo sh -c on the far side makes three shells and two levels. Counting the shells is the part people get wrong, which is why this tool asks where the command travels rather than asking for a number.
Why single quotes rather than backslashes or double quotes?
Inside single quotes a POSIX shell expands nothing at all: no variables, no backticks, no globs, no tilde. That makes the transformation identical at every layer, so it can simply be applied again for each shell in the path. Double-quote or backslash escaping needs a different rule at each layer depending on what survived the previous one, which is why hand-written attempts drift after the second hop.
Will $HOME use my machine or the remote one?
The remote one. The quoting protects the dollar sign from every shell along the way, so the variable survives intact and is expanded by the last shell, against the far end environment. The same is true of globs, backticks and a leading tilde. If you meant the local value, substitute it into the command before quoting. The tool prints a line naming the tokens it found so this is visible rather than assumed.
Why does a path with spaces split into two arguments on the server?
Because the quoting ran out one layer early. Each shell removes one level of quoting and then splits what remains on whitespace, so a path that was protected for the local shell arrives at the remote shell as bare text and is split there. The ladder in this tool shows the string at each stage, which is where a missing level becomes visible.
Is it safe to paste a command here?
Everything runs in your browser. The command is quoted by JavaScript on the page, nothing is uploaded, and the page works with no network once it has loaded. The command is saved in your browser local storage so it survives a reload, and the Clear button removes it.
What happens if my command already has a quoting mistake?
The tool reports it instead of quoting it. An unbalanced quote in the original command would still produce a valid-looking quoted string, which then fails at the far end with an error that points at the wrong place. Catching it before quoting is the only moment the mistake is still easy to read.