-
Notifications
You must be signed in to change notification settings - Fork 1.8k
SC3050
Vidar Holen edited this page Sep 8, 2020
·
1 revision
(or "In dash, ... is not supported." when using dash
)
ssh host "cat $(printf "%q" "$remotefile")"
There is not really a good, built-in way to escape a string for a remote shell in POSIX sh. However, you can replace each '
in the input with '\''
and then wrap the whole results in single quotes:
escape() { printf "'%s'\\n" "$(printf '%s' "$1" | sed -e "s/'/'\\\\''/g")"; }
ssh host "cat $(escape "$remotefile")"
Alternatively, switch to a shell that does support printf %q
like ksh
or bash
.
printf %q
is a bash and ksh extension. It's not supported on POSIX sh or dash.
If the command is gated by a check for the correct shell, you can ignore this warning.
- Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!