mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC3050 (markdown)
32
SC3050.md
Normal file
32
SC3050.md
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
## In POSIX sh, `printf %q` is undefined.
|
||||||
|
|
||||||
|
(or "In dash, ... is not supported." when using `dash`)
|
||||||
|
|
||||||
|
### Problematic code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
ssh host "cat $(printf "%q" "$remotefile")"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Correct code:
|
||||||
|
|
||||||
|
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:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
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`.
|
||||||
|
|
||||||
|
### Rationale:
|
||||||
|
|
||||||
|
`printf %q` is a bash and ksh extension. It's not supported on POSIX sh or dash.
|
||||||
|
|
||||||
|
### Exceptions:
|
||||||
|
|
||||||
|
If the command is gated by a check for the correct shell, you can [[ignore]] this warning.
|
||||||
|
|
||||||
|
### Related resources:
|
||||||
|
|
||||||
|
* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
|
Reference in New Issue
Block a user