mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Updated SC3004 (markdown)
36
SC3004.md
36
SC3004.md
@@ -1 +1,35 @@
|
|||||||
# SC3004: In POSIX sh, $".." is undefined
|
## In POSIX sh, $".." is undefined
|
||||||
|
|
||||||
|
### Problematic code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
printf $"Hello, %s\n" "$USER"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Correct code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
# Check if gettext exists
|
||||||
|
if ! type gettext > /dev/null 2>&1
|
||||||
|
then
|
||||||
|
# If not, create a dummy function that returns the input verbatim
|
||||||
|
gettext() { printf '%s' "$1"; }
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Use gettext to get a translated version of the string
|
||||||
|
printf "$(gettext 'Hello, %s\n')" "$USER"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Rationale:
|
||||||
|
|
||||||
|
Bash allows using `$"..."` to easily get a translated string according to the current locale. This is powered by the gettext infrastructure.
|
||||||
|
|
||||||
|
Neither `$".."` nor the gettext infrastructure or the `gettext` command is POSIX. A script targeting `sh` may choose to invoke `gettext` if the system has it (such as GNU or Solaris), or use the translated string verbatim if it doesn't (such as macOS or FreeBSD).
|
||||||
|
|
||||||
|
### Exceptions:
|
||||||
|
|
||||||
|
None.
|
||||||
|
|
||||||
|
### Related resources:
|
||||||
|
|
||||||
|
* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
|
Reference in New Issue
Block a user