Created SC3066 (markdown)

Vidar Holen
2025-04-11 15:04:20 -07:00
parent 44c787c795
commit 06e5ab67a5

32
SC3066.md Normal file

@@ -0,0 +1,32 @@
## In POSIX sh, test -G is undefined.
### Problematic code:
```sh
if [ -G file ]
then
echo "File is owned by your effective group"
fi
```
### Correct code:
```sh
if [ -n "$(find file -prune -group "$(id -g)")" ]
then
echo "File is owned by your effective group"
fi```
### Rationale:
`test -G` is a bash/ksh/dash/ash extension to check whether the file is owned by your effective group.
To ensure compatibility with other shells, you can use `find -group "$(id -g)"`.
### Exceptions:
If you expect your shell to support `test -G`, specify this explicitly in the shebang (or with `# shellcheck shell=dash` directive).
### Related resources:
* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!