mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
printf %b is POSIX, so move that suggestion into the Rationale section
17
SC2059.md
17
SC2059.md
@@ -26,8 +26,6 @@ The first printf writes `Unit test coverage: 96%`.
|
|||||||
|
|
||||||
The second writes ``bash: printf: `\': invalid format character``
|
The second writes ``bash: printf: `\': invalid format character``
|
||||||
|
|
||||||
### Exceptions
|
|
||||||
|
|
||||||
Sometimes you may actually want to interpret data as a format string, like in:
|
Sometimes you may actually want to interpret data as a format string, like in:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
@@ -35,9 +33,16 @@ octToAscii() { printf "\\$1"; }
|
|||||||
octToAscii 130
|
octToAscii 130
|
||||||
```
|
```
|
||||||
|
|
||||||
In Bash, Ksh and BusyBox, there's a `%b` format specifier that expands escape sequences without interpreting other format specifiers: `printf '%b' "\\$1"`. In POSIX, you can instead [[ignore]] this warning.
|
In this case, use the `%b` format specifier that expands escape sequences without interpreting other format specifiers:
|
||||||
|
|
||||||
Other times, you might have a pattern in a variable:
|
```sh
|
||||||
|
octToAscii() { printf '%b' "\0$1"; }
|
||||||
|
octToAscii 130
|
||||||
|
```
|
||||||
|
|
||||||
|
### Exceptions:
|
||||||
|
|
||||||
|
Sometimes you might have a pattern in a variable:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
filepattern="file-%d.jpg"
|
filepattern="file-%d.jpg"
|
||||||
@@ -45,3 +50,7 @@ printf -v filename "$filepattern" "$number"
|
|||||||
```
|
```
|
||||||
|
|
||||||
This has no good rewrite. Please [[ignore]] the warning with a [[directive]].
|
This has no good rewrite. Please [[ignore]] the warning with a [[directive]].
|
||||||
|
|
||||||
|
### Related resources:
|
||||||
|
|
||||||
|
* https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/printf.html
|
Reference in New Issue
Block a user